Getting Resource id #3 Error in MySql - php

I ran this code and I got a Resource id #3 error where it should have showed the full movies table.
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
echo $data;

This is not an error Your query is getting executed and you are getting appropriate resource from mysql_query() as it should be returned.
To get the response you have to use mysql_fetch_array() or mysql_fetch_assoc()
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
print_r($row);
}
SUGGESTION: mysql_* are no longer maintained .Try switching to mysqli_* or PDO

You're not getting an error, the MySQL API is just doing what you're asking it to: echoing the contents of $data, which is a MySQL query resource at this point. Extend the code to actually retrieve the results:
while($row = mysql_fetch_object($data))
var_dump($row);
And you'll see the output.
Note that the mysql_* API is deprecated since PHP 5.5 by the way.

Resourse id #3 means that the $data variable was used to open a resourse, it is not an error.
If you were to open another resourse, like a file, using:
$var=fopen('myfile','a+');
echo $var;
You would get Resourse id $4 as a result.
So to get the output you desire, you need to use a loop.
It is described in here.

Related

Generate simple JSON file based on database

Hey I am iOS developer I am trying to create simple JSON output from my website. I found good start link and here is some explanation how to do it.
So I've created accounts.php file and put it to my public_html folder
<?php
include_once("JSON.php");
$json = new Services_JSON();
$link = mysql_pconnect("localhost", "user", "pass") or die("Could not connect");
mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
Echo $json->encode($arr);
?>
Of course I use my user and password and I pointed my just created database ob my end.
so when I try to request my file so http//mywebsite.com/accounts.php there is no data.
I tried to use google chrome and Postman so it says No response received when I switch to JSON. For HTML there is no info in Postman.
My question how can I test it? even if I use Echo(123) before include_once("JSON.php"); line there is no 123 on html page.
I tried to test PHP with only this code:
<?php
phpinfo();
?>
and it works. I have PHP Version 5.4.32
First of all, simply use PHP's function json_encode($arr). It does exactly what you are asking for and is pretty much included in every version of PHP that I can think of.
Documentation
Also, I am not sure if this is the issue, but you may want to change Echo ==> echo. This is generally convention at the very least.
SUPER IMPORTANT
Finally, DO NOT USE mysql extension. Its is dangerous, may not work correctly, and has security vulnerabilities. Use mysqli or PDO.
Matrosov -
You are very close. Use the json_encode function to output your code via the PHP manual. Also consider using mysqli instead of mysql for your database connection as it has been better support for modern MySQL servers.
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/book.mysqli.php
<?php
include_once("JSON.php");
$link = mysqli_connect("localhost", "user", "pass") or die("Could not connect");
$link->mysql_select_db("iglobe") or die("Could not select database");
$arr = array();
$rs = mysql_query("SELECT * FROM users");
while($obj = mysql_fetch_object($rs)) {
$arr[] = $obj;
}
echo json_encode($arr);
?>

Include no database selected

I have a project (exisiting) and I am ordered to continue it
but there's something strange
in my connection
<?php
include "adodb5/adodb.inc.php";
$config['mysqlhost'] = 'localhost';
$config['mysqluser'] = 'xxx';
$config['mysqlpass'] = 'yyy';
$config['mysqldb'] = 'zzz';
$DB = ADONewConnection('mysql');
$DB->Connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass'],$config['mysqldb'],true);
?>
and if I try to call query (same queries as below) from this page, it works (and when I echo, it shows the value)
So I go to other page
<?
include ("inc/con.php");
?>
<?php
$sql = ("SELECT * FROM table");
$query = mysql_query($sql)or die($myQuery."<br/><br/>".mysql_error());
$result = mysql_fetch_array($query);
echo $result ['table id'];
?>
and the result is
Notice: Undefined variable: myQuery in C:\xampp\htdocs\d88\www\mypage.php on line 9
No database selected
is there anything wrong with it?
since i try on con page, it works and when i include it to other page, it not working
You are not defining any $myQuery either in inc/con.php nor in the same file itself. Also you are not selecting any database with mysql_select_db:
mysql_select_db($config['mysqldb']);
You are suggest, also, not to use mysql_* functions as they are going to be deleted and are yet deprecated (and you can use PDO or mysqli).
Notice: I think $sql = ("SELECT * FROM table") gets evaluated as $sql = true.
You can not connect with ADODB connection and establish a query with mysql_query.
the syntax is something like this mysql_query ($query ,$con). $con is optional but if you do not specify it, the last link opened by mysql_connect() is assumed; but you have not any mysql_connect() statement before
because of my version of php, i must use <?php ?> instead of <? ?>
thanks for helping

Cannot Display Data from MySQL table

I've got a pretty standard call to a MySQL database and for some reason I can't get the code to work. Here's what I have:
$mysqli = mysqli_connect("localhost","username","password");
if (!$mysqli)
{
die('Could not connect: ' . mysqli_error($mysqli));
}
session_start();
$sql = "SELECT * FROM jobs ORDER BY id DESC";
$result = $mysqli->query($sql);
$num_rows = mysqli_num_rows($result);
Now, first, I know that it is connecting properly because I'm not getting the die method plus I added an else conditional in there previously and it checked out. Then the page displays but I get the errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 11
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in blablabla/index.php on line 12
I've double-checked my database and there is a table called jobs with a row of "id" (it's the primary row). The thing that confuses me is this is code that I literally copied and pasted from another site I built and for some reason the code doesn't work on this one (I obviously copy and pasted it and then just changed the table name and rows accordingly).
I saw the error and tried:
$num_rows = $mysqli_result->num_rows;
$row_array = $mysqli_result->fetch_array;
and that fixed the errors but resulted in no data being passed (because obviously $mysqli_result has no value). I don't know why the error is calling for that (is it a difference in version of MySQL or PHP from the other site)?
Can someone help me track down the problem? Thanks so much. Sorry if it's something super simple that I'm overlooking, I've been at it for a while.
You didn't selected the database
$mysqli = mysqli_connect("localhost","username","password","database");
The problem is you haven't selected the database.
use this code for select database.
$mysqli = mysqli_connect("localhost","username","password");
mysqli_select_db("db_name",$mysqli);
You have to select database in order to fire mysql queries otherwise it will give you error.
I believe that schtever is correct, I do not think you are selecting the database. It isn't in the code snip and if you search online you see other people with similar errors and it was because the database wasn't selected. Please let us know if you selected a database before anything else is checked. Thanks.
Try this:
session_start();
$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno)
{
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
$mysqli->close();
}
$query ="SELECT * FROM jobs ORDER BY id DESC";
$values = $mysqli->query($query);
if($values->num_rows != 0)
{
while($row = $values->fetch_assoc())
{
//your results echo here
}
}
else
{
//if no results say so here
}
See this manual for mysqli_connect you can select the database right in this function.

mysql_real_escape_string() expects parameter 1 to be string

require("includes/connect.php");
$result = mysql_query("SELECT * FROM entries", $link);
while ($row = mysql_fetch_array($result)) {
htmlentities($row['quotes']);
}
I am trying to display data that is in the database, but I keep on getting:
Warning: mysql_real_escape_string() expects parameter 1 to be string
Is there anything wrong in the above code that is causing the problem? I am quite new to PHP and I am trying to understand what's going on and why it's doing it.
connect.php
$link = mysql_connect("localhost", "root", "");
if (!$link) {
die("Could not connect to the db");
}
mysql_select_db("ENTRIES", $link);
(I'm working on this locally, so user/pass really isn't important right now)
I don't see the point with escaping the above query, but you could do it like this:
$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);
You should read the documentation: mysql_real_escape_string()
As the error explains mysql_real_escape_string() takes a string as a parameter. In your code you posted as a comment you are passing $link which isn't a string, it's a database connection.
As #kristen, has said to solution should be to wrap you sql statement like so
$result = mysql_query(mysql_real_escape_string("SELECT * FROM entries"), $link);
If you are still receiving the error after this, you must be using the function elsewhere.

mysql_fetch_object(): supplied argument is not a valid MySQL result resource

I'm trying to set a simple cron script to do some database updating, and I'm pretty worthless with MySQL without ActiveRecord (I use CodeIgniter). I keep getting the error message,
mysql_fetch_object(): supplied argument is not a valid MySQL result resource
with the following code:
mysql_connect("localhost", "user", "pass") or die(mysql_error());
mysql_select_db("my_db") or die(mysql_error());
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query)
or die("Query failed: ".mysql_error()." Actual query: ".$query);
while($row=mysql_fetch_object($result))
{
....
}
Like I said, I'm not that great with straight PHP and MySQL (would appreciate any advice on how to include some sort of framework or ActiveRecords that could be used as part of a cron job). Any thoughts?
What happens if you assign mysql_connect as a variable and pass it in as a link identifier as a second parameter to the mysql_select_db and query functions?
$db = mysql_connect("localhost", "user", "pass") or die(mysql_error());
// Check to see if valid connection
var_dump($db);
mysql_select_db("my_db", $db) or die(mysql_error());
$query = "select visit_e_id, visit_e_type from visits";
$result = mysql_query($query, $db)
or die("Query failed: ".mysql_error()." Actual query: ".$query);
while($row=mysql_fetch_object($result))
{
....
}
Also ensure that you query syntax is correct and doesn't have misspelling or typos
This error is pretty simple and self-explanatory - a $result variable has unexpected type.
Thus, you have to so some debugging. Add var_dump($result); before and inside loop and study the output.

Categories