PHP: Warning: mysql_fetch_array() expects parameter 1 to be resource - php

I got this error while adding this code. Would appreciate some help. It's for a CS jackpot site.
$sitename = "website.com"; // YOUR DOMAIN
$link = mysql_connect("localhost", "db_user", "db_pass"); // MYSQL , LOCALHOOST , USERNAME , PASSWORD
$db_selected = mysql_select_db('db_name', $link); // MYSQL DATABASE
mysql_query("SET NAMES utf8");
function fetchinfo($rowname,$tablename,$finder,$findervalue) {
if($finder == "1") $result = mysql_query("SELECT $rowname FROM $tablename");
else $result = mysql_query("SELECT $rowname FROM $tablename WHERE `$finder`='$findervalue'");
while($row = mysql_fetch_assoc($query))
return $row[$rowname];
}

Some tips:
Use mysqli better than mysql
Split the vars in the query, like "SELECT ".$rowname." FROM ".$tablename;

Hope this help...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('host','username','password','database_name');
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//MySqli Select Query
$result = $mysqli->query("SELECT id, product_name FROM products");
while($row = $results->fetch_assoc()) {
echo $row["id"].' - '.$row["product_name"].'<br>';
}
$results->free();
$mysqli->close();

Related

Why query not working in from my php

I have query, and i have database. When I query in dbforge(mysql) i got result. When from php, i got nothing. Can't understand why? If I write query simple like "select * from table where id = 1", its working.
Below my code:
<?php
$conn = mysqli_connect("localhost", "username", "pass", "db");
ini_set('max_execution_time', 300);
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($conn, "SET CHARACTER SET utf8 ");
$result = mysqli_query($conn, "Select distinct level_two FROM reportls._report_sales_report Where level_one = 'Музыка'");
if($result == false)
{
die('INVALID QUERY: '.mysqli_error($conn));
echo "error";
}
while ($data = mysqli_fetch_row($result))
{
if(!empty($data[0]))
$sendBack = $sendBack."<option value=\"$data[0]\">$data[0]</option>";
}
echo ($sendBack);
$sendBack = '';
mysqli_free_result($result);
?>
This code return nothing. But if I paste my query into my dbforge, it returns rows.
Could you help me, or write some hits? What i wrote not correct?

No database selected on PHP

I got this code to create a JSON file with the data I have in my database:
<?php
$connect = mysql_connect("localhost", "root", "123", "boxr") or die("Error connecting with the host.");
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
$response = array();
$users = array();
$result = mysql_query ($sql);
while ($row = mysql_fetch_array($result)) {
$gamertag = $row['gamertag'];
$banned = $row['banned'];
$users[] = array('gamertag' => $gamertag, 'banned' => $banned);
}
$response['users'] = $users;
$fp = fopen('users.json', 'w');
fwrite($fp, json_encode($response));
fclose($fp);
?>
The problem is when I run the script it shows the following:
No database selected
I checked my code twice but I can't figure out why its telling me that.
You have to select the database name with:
mysql_select_db ('your_db_name');
Well you can do it like that:
$connect = mysql_connect("localhost", "root", "123") or die("Error connecting with the host.");
mysql_select_db("boxr", $connect);
And later, correct it:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($result))...
By:
$sql = mysql_query("SELECT * FROM users") or die(mysql_error());
while ($row = mysql_fetch_array($sql))...
If you are calling the function with the name $sql, you can't later put the name of $result.
mysql_* functions are deprecated but you could change your query
$sql = mysql_query("SELECT * FROM dbname.users") or die(mysql_error());
Use :
$db_selected = mysql_select_db('db_name', $connect);
if (!$db_selected) {
die ('Can\'t use db_name : ' . mysql_error());
}

MySQL Query and display not working?

My MySQL database is setup with the name "chatterr" with the table name, "maillist". I am currently using the php below:
<?php
mysql_connect("localhost","root","");
//query databae
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows) {
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result, $row, "first_name");
$last_name=mysql_result($result, $row, "last_name");
?>
<?php echo $id; ?>
<?php
$row++;
}
?>
It works on localhost but doesn't work in PHP. What's wrong with the code?
Select a database with mysql_select_db before querying it
mysql_connect("localhost","root","");
mysql_select_db("chatterr");
or specify the database name in the query
$query = "SELECT * FROM chatterr.students ORDER BY id DESC LIMIT 4";
UPDATE: Besides that your is connection probably failing. Change
mysql_connect("localhost","root","");
to
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
to see if that's the case.
UPDATE3: Put it all together. Although that code has a LOT room for improvement it works perfectly fine on my machine.
<?php
$db = mysql_connect("localhost","root","");
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db("chatterr", $db);
if (!$db_selected) {
die ('Could not select db: ' . mysql_error());
}
//query databae
$query = "SELECT * FROM test.students ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die('Error, insert query failed');
$row=0;
$numrows=mysql_num_rows($result);
while($row<$numrows)
{
$id=mysql_result($result,$row,"id");
$first_name=mysql_result($result,$row,"first_name");
$last_name=mysql_result($result,$row,"last_name"); ?>
<?php echo $id; ?>
<?php
$row++;
}
?>
And please, don't use mysql_* functions for new code. They are
deprecated. Use prepared
statements with either PDO
or MySQLi.
Just do it this way :
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("chatterr",$con) or die(mysql_error());
//query database
$query = "SELECT * FROM maillist ORDER BY id DESC LIMIT 4";
$result=mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$first_name = $row["first_name"];
$last_name = $row["last_name"];
}
?>

php switching to mysqli: num_rows issue

I recently started updating some code to MySQL improved extension, and have been successful up until this point:
// old code - works
$result = mysql_query($sql);
if(mysql_num_rows($result) == 1){
$row = mysql_fetch_array($result);
echo $row['data'];
}
// new code - doesn't work
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]");
if($result->num_rows == 1) {
$row = $result->fetch_array();
echo $row['data'];
}
As shown I am trying to use the object oriented style.
I get no mysqli error, and vardump says no data... but there definitely is data in the db table.
Try this:
<?php
// procedural style
$host = "host";
$user = "user";
$password = "password";
$database = "db";
$link = mysqli_connect($host, $user, $password, $database);
IF(!$link){
echo ('unable to connect to database');
}
ELSE {
$sql = "SELECT * FROM data_table LIMIT 1";
$result = mysqli_query($link,$sql);
if(mysqli_num_rows($result) == 1){
$row = mysqli_fetch_array($result, MYSQLI_BOTH);
echo $row['data'];
}
}
mysqli_close($link);
// OOP style
$mysqli = new mysqli($host,$user, $password, $database);
$sql = "SELECT * FROM data_table LIMIT 1";
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); /* I have added the suggestion from Your Common Sence */
if($result->num_rows == 1) {
$row = $result->fetch_array();
echo $row['data'];
}
$mysqli->close() ;
// In the OOP style if you want more than one row. Or if you query contains more rows.
$mysqli = new mysqli($host,$user, $password, $database);
$sql = "SELECT * FROM data_table";
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]"); /* I have added the suggestion from Your Common Sence */
while($row = $result->fetch_array()) {
echo $row['data']."<br>";
}
$mysqli->close() ;
?>
As it was said, you're not checking for the errors.
Run all your queries this way
$result = $mysqli->query($sql) or trigger_error($mysqli->error." [$sql]");
if no errors displayed and var dumps are saying no data - then the answer is simple: your query returned no data. Check query and data in the table.
In PHP v 5.2 mysqli::num_rows is not set before fetching data rows from the query result:
$mysqli = new mysqli($host,$user, $password, $database);
if ($mysqli->connect_errno) {
trigger_error(sprintf(
'Cannot connect to database. Error %s (%s)',
$mysqli->connect_error,
$mysqli->connect_errno
));
}
$sql = "SELECT * FROM data_table";
$result = $mysqli->query($sql);
// a SELECT query will generate a mysqli_result
if ($result instanceof mysqli_result) {
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
$num_rows = $result->num_rows; // or just count($rows);
$result->close();
// do something with $rows and $num_rows
} else {
//$result will be a boolean
}
$mysqli->close() ;

PHP not finding mySQL database?

I must be doing something wrong
I have a very simple script and a very simple database
No idea why it's not working..
Please help
<?php
error_reporting(E_ALL);
$link = mysql_connect('localhost', 'root', 'password');
if(!$link)
{
die('Could not connect: ' . mysql_error());
}
$database = mysql_select_db('test_db', $link);
if(!$database)
{
die('Could not connect to database: ' . mysql_error());
}
$result = mysqli_query($link, "SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysqli_fetch_array($result);
echo $row[0];
?>
It's not even giving any errors, just echoing 'Problem'...
The database connects fine, and there is 1 user in the database with an ID of 1 and forename is Cristian.
In $result = mysqli_query try just useing $result = mysql_query same in $row = mysqli_fetch_array should help
Try replaceing you code from $result onward with this
$result = mysql_query("SELECT forename FROM users WHERE id='1'");
if(!$result)
echo 'PROBLEM';
$row = mysql_fetch_array($result);
echo $row[0];
?>

Categories