What is resource id #170? - php

what i am trying to achieve here is that i want to retrieve data from Joomla's database.. Using the code, below, i am able to connect to Joomla's database successfully.. But the problem is, $query1 displayed this output, "resource id #170".. We tried connecting to different tables to see if we are able to get different output, but same output appear.. So i am wondering, what does that message means? Is it somethings wrong with the code, or database or etc..
<?php
session_start();
ob_start();
?>
<html>
<head>
<body>
<?php
include 'config.php';
//This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
echo $dbname;
echo ('connected successfully!!!');
?>
<?php
$query1 = "select speciesName from jos_mainDBspecies where species_id ='1'";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_assoc($result1);
extract($row1);
echo $result1;
?>
Many thanks in advance for helping!

$result1 is a MySQL result resource. It is supposed to be like this. Your actual result is in $row1 in your code.

Related

PHP: json_encode() doesn't show anything with multidimensional array

I'm trying to make a simple PHP script that fetches a table from my MySQL database and encodes the results in JSON, so I can use them later in Java.
This is my code:
<?php
$servername = "localhost:3036";
$username = "example_user";
$password = "example_password";
$conn = mysql_connect($servername, $username, $password);
if(! $conn) {
die("Could not connect: " . mysql_error());
}
$sql = "SELECT * FROM table_name";
mysql_select_db("database_name");
$retval = mysql_query($sql, $conn);
if(! $retval) {
die("Could not get data: " . mysql_error());
}
while($row = mysql_fetch_assoc($retval)) {
$output[]=$row;
}
print(json_encode($output));
mysql_close($conn);
?>
This just gives a blank page as output (error messages are set to display).
However, if I change json_encode($output) to json_encode($output[0]) (or any other number within the array's bounds), the output becomes that one $row array.
This is probably a really stupid question, but after about 3 hours of research I'm at my wit's end. Thank you for any help.
User #Joni led me to the solution.
Adding mysql_set_charset("utf8") fixed my issue.
As mentioned in this post: Why is this PHP call to json_encode silently failing - inability to handle single quotes?.
Try
echo json_encode($output) ;
It seems you have some utf8 character in your result set
add this statement before running your query
mysql_query('SET CHARACTER SET utf8');
Update
"mysql"
to
"mysqli"
and add
mysqli_set_charset($variável_de _conexão, 'utf8');
below the connection variable

PHP include for mysql query without using my_sql_connect

I'm attempting to create a dropdown list that is populated using PHP by gathering all of my clients in my database. This is working, but the problem is the code I am using requires the my_sql_connect and my_sql_select_db login details to populate the list.
On the pages this feature is required, the following snippet of code is provided at the top of each page to connect to the database:
require_once ('includes/dbConfig.php');
The code that is working is below (the #'s substitute my login details):
mysql_connect('localhost', '#', '#');
mysql_select_db('#');
$sql = "SELECT client_organisation_name FROM clients";
$result = mysql_query($sql);
echo "<select name='clients'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['client_organisation_name'] ."'>" . $row['client_organisation_name'] ."</option>";
}
echo "</select>";
What I am having difficulty in achieving is the dropdown list to populate using the connection already established. I have used the following code to try and substitute this but to no success:
$conn = mysql_connect('$dbhost', '$dbuser', '$dbpass') or die ('Error connecting to mysql');
mysql_select_db('$dbname');
I have searched the internet but have not found a suitable solution. For obvious reasons, it is a security risk and hassle to have your database login details evident, therefore I would be grateful for some help to get my snippet of code to connect to my database.
You can use MySQLi:
define(DB_HOST, "yourhost"); // Host
define(DB_NAME, "yourdbname"); // Database
define(DB_USER, "youruser"); // Username
define(DB_PASS, "yourpass"); // Password
$mysqli=new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die("Error " . mysqli_error($link));
mysqli_set_charset($mysqli, 'utf8');
$results = $mysqli->query("SELECT client_organisation_name FROM clients");
while(($array_results[] = $results->fetch_assoc()) || array_pop($array_results));
echo '<select name="clients">'."\n";
foreach($array_results as $client) {
echo '<option value="'.$client['client_organisation_name'].'">'. $client['client_organisation_name'].'</option>'."\n";
}
echo '</select>'."\n";
Make sure that the connection data (user, password, name and host) are correct.
The single quote strings don't evaluate the variables inside.
So your code should be:
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
EDIT:
I saw #Jason McCreary's comment and I have to agree with him.
You should definitely use mysqli. The only changes in your code are following:
$db = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
Then you execute the mysql queries like this:
$db->query("INSERT MYSQL CODE HERE");

How can I get a specific data using SQL select query?

I am trying to make a blog site.For this purpose I need to use a specific data from a specific field from my database table.To do that I wrote these code.
<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");
$img = "select image from news where uid=1";
echo $img;
?>
My database connection is OK.It should print like this user_img1.jpg. But it prints the whole sql query like select image from news where uid=1. I run this code on phpmyadmin. It works! But it does not work in my php script.How can I do now?
You can not give the query as it is and expect result like in phpadmin.
For this first of all you have to connect to your DB like this
$con = mysqli_connect("localhost","my_user","my_password","my_db");
execute required query like this
$query22 = "select image from news where uid = 1";
$result22 = mysqli_query($con, $query22) or die (mysqli_error());
Get the result and display like this
while($rows = mysqli_fetch_array($result22, MYSQLI_BOTH))
{
echo "<br>Values in db: " . $rows['columnname'];
}
Also i advice you to take a look at these tutorials
http://codular.com/php-mysqli
http://www.dreamincode.net/forums/topic/54239-introduction-to-mysqli-and-prepared-statements/
Please read some PHP 101 kind of tutorials on how to use PHP.
To get data from DB (in almost any language)
You need to connect to a DB. The connection gets you some sort of resource
You formulate your query (which you seem to have done)
You execute the query against the DB that you connected to (step #1)
You get a result (set)
You iterate over the result set to get the individual result(s); in your case the result set would be just one result (or row).
The examples to do this in PHP are very basic; please do your own lookup on net. This one seems good enough to get you started - http://www.w3schools.com/php/php_mysql_intro.asp
Try this,
<?php
$host = "localhost";
$user = "root";
$pass = "12345";
$db = "bnsb";
$conn = mysql_connect($host, $user, $pass) or die("Connection Failed!");
mysql_select_db($db, $conn) or die("Database couldn't select!");
$img = "select image from news where uid=1";
$result=mysql_query($img);
while($row=mysql_fetch_array($result)){
echo '<img src="your_path_to_image/'.$row['image'].'" /> - '.$row['image'];
}
?>

PHP MYSQL Access denied for user Error [duplicate]

This question already has answers here:
Unable to connect to database: Access denied for user ''#'localhost' to database 'socialdb'
(6 answers)
Closed 9 years ago.
I'm trying to get the 'Content' value from a specific row ID in mySql, I echoed the result in PHP, But i got "Access denied for user ''#'localhost' (using password: NO)".
I tryed to check my query in phpMyAdmin, and i got the exact result I expected for.
Here is my code:
$con = mysql_connect("localhost", "XXX", "XXX") or die (mysql_error());
$db = mysql_select_db("XXX", $con) or die (mysql_error());
function head($d) {
$query = mysql_query("SELECT Content FROM list WHERE ID = '$d'") or die(mysql_error());
$post = mysql_fetch_assoc($query);
echo $post["Content"];
}
<h1> <?php head(1); ?> </h1>
Thank you so much.
The error message you quote sounds like the database isn't connected yet when you're making the call. If no connection exists, mysql_query() will try to establish a connection with default values (which usually are empty).
You should check your program flow and make sure you are actually connected to the database before making any queries.
move your first two lines to your function:
function head($d) {
$con = mysql_connect("localhost", "XXX", "XXX") or die (mysql_error());
$db = mysql_select_db("XXX", $con) or die (mysql_error());
$query = mysql_query("SELECT Content FROM list WHERE ID = '$d'") or die(mysql_error());
$post = mysql_fetch_assoc($query);
echo $post["Content"];
}
<h1> <?php head(1); ?> </h1>
issue is with
mysql_connect("localhost", "ncode_hana", "NOAM7778")
chech that sqn user and pass are correct and this user has right over your selected db...
also, never share your orignial user ID and password with anyone..
also, as Pekka told you need something like this
<h1> <?php include('yourdatabaseconnectionfile.php'); ?> <?php head(1); ?> </h1>
ALTERNATIVELY
<?php
$con = mysql_connect("localhost", "XXX", "XXX") or die (mysql_error());
$db = mysql_select_db("XXX", $con) or die (mysql_error());
<h1> <?php head(1); ?> </h1>
function head($d) {
$query = mysql_query("SELECT Content FROM list WHERE ID = '$d'") or die(mysql_error());
$post = mysql_fetch_assoc($query);
echo $post["Content"];
}
ONE MORE OPTION
add connection string in function (not recommended though)
function head($d) {
mysql_connect("localhost", "ncode_hana", "NOAM7778");
$query = mysql_query("SELECT Content FROM list WHERE ID = '$d'") or die(mysql_error());
$post = mysql_fetch_assoc($query);
echo $post["Content"];
}

simple php - Mysql search not working properly

I have this code:
<?php
// Make a MySQL Connection
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM clients WHERE FNAME='".$_POST['clientsearch']."' OR LNAME='".$_POST['clientsearch']."' OR MAIL='".$_POST['clientsearch']."' OR TEL='".$_POST['clientsearch']."'"")
or die(mysql_error());
// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry
echo "FName: ".$row['FNAME'];
echo "LNAME: ".$row['LNAME'];
echo "FName: ".$row['MAIL'];
echo "LNAME: ".$row['TEL'];
?>
The goal is to search my mysql database to find the result of $_POST['clientsearch'] in one of the fields and return the lines that have that word in it (it is always 1 word)
If I use this:
$result = mysql_query("SELECT * FROM clients WHERE FNAME='".$_POST['clientsearch']."'"")
it seems to work. but it only searches in the FNAME column, not all of them. Also I only get the first result back. not all.
I'm starting php/mysql so I'm a little lost and don't know all functions yet. Could someone explain how I could fix my code up?
Thanks a lot for your help :)
For starters, you'll need to loop through each row in your result set if you're expecting more than 1 row. I illustrate how to do this with your original code.
<?php
// Make a MySQL Connection
$dbhost = 'xxx';
$dbuser = 'xxx';
$dbpass = 'xxx';
$dbname = 'xxx';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
// Retrieve all the data from the "example" table
$result = mysql_query("SELECT * FROM clients WHERE FNAME='".$_POST['clientsearch']."' OR LNAME='".$_POST['clientsearch']."' OR MAIL='".$_POST['clientsearch']."' OR TEL='".$_POST['clientsearch']."'"")
or die(mysql_error());
// Print out the contents of the entry for each row in result
while( $row = mysql_fetch_array( $result, MYSQL_ASSOC ) ) {
echo "FName: ".$row['FNAME'];
echo "LNAME: ".$row['LNAME'];
echo "FName: ".$row['MAIL'];
echo "LNAME: ".$row['TEL'];
}
?>
Take a look in the PHP documentation on mysql_real_escape_string for starters on the injection stuff.
Also, as others stated you may be looking for the LIKE instead of = SQL syntax. Also, look into the % wild card for LIKE.
Not an answer to the question, but hopefully helpful. Try writing code like this:
$cs = mysql_escape_string($_POST['clientsearch']);
$result = mysql_query("
SELECT
*
FROM clients
WHERE
FNAME='$cs'
OR LNAME='$cs'
OR MAIL='$cs'
OR TEL='$cs'
");
Exactly how you indent is up to you. This approach helps a great deal with readability, and hence also debugging :)

Categories