</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/17498216/can-i-mix-mysql-apis-in-php" dir="ltr">Can I mix MySQL APIs in PHP?</a>
<span class="question-originals-answer-count">
(4 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2020-01-18 05:17:34Z" class="relativetime">3 months ago</span>.</div>
</div>
</aside>
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: <br>";
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 )
</div>
You are using mysqli_connect
, but mysql_query
. You can't mix mysqli
and mysql
- use one or the other. Most people will tell you to use mysqli
. I'd say to use whichever is best for you. In my case, that's mysql
because I don't have to pass a connection reference to every single call... among other reasons.
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']."<br />"; //you need to put the column you want to output.
As mysql is depricated try using mysqli, pdo or any other. Code for mysqli is as follow:
<?php
$mysqli = new mysqli("host", "username", "password", "database_name");
$test= $mysqli->query("SELECT * FROM chat") or die($mysqli->error);
if($test->num_rows){
while($rows = $test->fetch_array(MYSQLI_ASSOC)){
$data1= $rows['data1'];
$data2= $rows['data2'];
}
}
?>
$test->num_rows checks for number of results and while loop is used to get all the data returned.