Jump to content

How to change syntax from mysqli to mysql?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
I found a autosuggest code, but the connection to database is mysqli, so I edit the syntax to mysql_connect..

Here is the original syntax:

<?php

   $db = new mysqli('DB_HOST', 'USERNAME' ,'PASSWORD', 'DATABASE_NAME');

	

	if(!$db) {

	

		echo 'Could not connect to the database.';

	} else {

	

		if(isset($_POST['queryString'])) {

			$queryString = $db->real_escape_string($_POST['queryString']);

			

			if(strlen($queryString) >0) {


				$query = $db->query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");

				if($query) {

				echo '<ul>';

					while ($result = $query ->fetch_object()) {

	         			echo '<li onClick="fill(\''.addslashes($result->country).'\');">'.$result->country.'</li>';

	         		}

				echo '</ul>';

					

				} else {

					echo 'OOPS we had a problem :(';

				}

			} else {

				// do nothing

			}

		} else {

			echo 'There should be no direct access to this script!';

		}

	}

?>


and I edit it:


<?php

 $db_host         = 'localhost';

$db_user         = 'root';

$db_password     = '';

$db_name         = 'db_upload';

    

    

$db = mysql_connect($db_host , $db_user ,$db_password, $db_name);

	

		if(isset($_POST['queryString'])) {

			$queryString = mysql_real_escape_string($_POST['queryString']);

			

			if(strlen($queryString) >0) {


				$query = mysql_query("SELECT country FROM countries WHERE country LIKE '$queryString%' LIMIT 10");

				if($query) {

				echo '<ul>';

					while ($result = mysql_fetch_object($query)) {

	         			echo '<li onClick="fill(\''.addslashes($result->country).'\');">'.$result->country.'</li>';

	         		}

				echo '</ul>';

					

				} else {

					echo 'OOPS we had a problem :(';

				}

			} else {

				// do nothing

			}

		} else {

			echo 'There should be no direct access to this script!';

		}

?>


when i run it, it was always fall in else statement

'OOPS we had a problem :('
even I input correct data.

here is the link where I found the code:
jQuery PHP Ajax Autosuggest | Ashley Ford - Tutorials :: jQuery :: PHP :: CSS :: HTML5 :: Papermashup.com

Thank you

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
And your php don't support MySQLi? MySQLi is just another version of the MySQL-driver in php, where i stands for improved. it's better to use MySQLi if possible. just keep it if you can. it works just as good with the same server.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 474 posts
Yes, my php doesn't support mysqli, so i tried to change the syntax, from mysqli to mysql


Thank you




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users