Wednesday, 21 June 2017

php - mysql queryes not work



Have code in alone sript for ajax including,




   $connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");
echo "test:
";
print_r($test);



but it returns nothig, if i call:



//db connect
$connection = mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)
die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysql_query("SELECT * FROM chat");


print_r($connection);


returns:



mysqli Object ( [affected_rows] => -1 [client_info] => mysqlnd 5.0.11-dev - 20120503 - $Id: 40933630edef551dfaca71298a83fad8d03d62d4 $ [client_version] => 50011 [connect_errno] => 0 [connect_error] => [errno] => 0 [error] => [error_list] => Array ( ) [field_count] => 0 [host_info] => localhost via TCP/IP [info] => [insert_id] => 0 [server_info] => 5.6.13-log [server_version] => 50613 [stat] => Uptime: 7999358 Threads: 4 Questions: 656648624 Slow queries: 9090 Opens: 3348364 Flush tables: 1 Open tables: 1999 Queries per second avg: 82.087 [sqlstate] => 00000 [protocol_version] => 10 [thread_id] => 5759563 [warning_count] => 0 )


Answer



you connected by mysqli and you used mysql.




change that



  $test = mysql_query("SELECT * FROM chat");


to



 $test = mysqli_query($connection,"SELECT * FROM chat");



and then you need to fetch your query like that



 $row= mysqli_fetch_array($test) ;
echo $row['some_column'];


this is whole code:



 $connection =mysqli_connect($db_config['server'],$db_config['db_user'],$db_config['db_pass']);
if (!$connection)

die('Could not connect to database: ' . mysql_error());
mysqli_select_db($connection, $db_config['db_name']);

$test = mysqli_query("SELECT * FROM chat");
$row= mysqli_fetch_array($test) ;
echo $row['some_column']."
"; //you need to put the column you want to output.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...