How to select database and use JOIN with MySQL PHP database - php

I'm trying to run this function but can't get it to work, what am I doing wrong (output says my fetch array arg is invalid)?
$link = mysql_connect('localhost', 'over_app', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) { echo "Your Pic ID: $row['PicID']"; }
Note The Pic table is in the Pics database and the Seen table is in the SeenPics database

mysql_query returns a boolean false is the query fails .. do this
$result = mysql_query("SELECT Pic.PicID FROM Pic LEFT JOIN Seen ON Pic.PicID = Seen.PicID");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = mysql_fetch_array($result)) {
echo "Your Pic ID: $row['PicID']";
}
Don't use mysql_* See this comment
Update
Now that we have the full facts - ie your attempting a join across multiple databases ... the problem is that your query isnt complete ... try this :
SELECT Pic.PicID FROM Pics.Pic Pic LEFT JOIN SeenPics.Seen Seen ON Pic.PicID = Seen.PicID

Related

Display Bids from database in order

Ive been trying to display a "bid" from the database to no success.
here is my error
Fatal error: Function name must be a string in /home/rslistc1/public_html/get-bids.php on line 7
here is my code
<?php
include('session.php');
?>
<?php
require_once('mysql_connect.php');
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
$result3 = mysql_query($query3) OR die($mysql_error());
$num = mysql_num_rows($result3);
while ($row = mysql_fetch_array($result3, MYSQL_ASSOC)) { ?>
<?php echo''.$row['bid'].'';
}
?>
Any idea
Before we address the line 7 issue, lets check other errors. In order to request a query to a MYSQL database, we need to create a connection:
$con = mysqli_connect("ip_address","user","password","database_name");
Once we have that connection, let us check if we can actually connect to the database:
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
Appreciate that mysqli_error() function uses the connection. Now the query string:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '$login_session'";
You are sending a query to look for a username called "$login_session" and it would most likely not find any match. To add strings from variables will be as follow:
$query3 = "SELECT id, username, bid FROM bids WHERE username = '" . $login_session . "'";
Now, for the error in line 7
result3 = mysql_query($con, $query3) OR die($mysql_error($con));
As you can see, both mysql function use the connection to check for errors. Try it and let me know if everything works fine.
Edit:
Terribly sorry my friend, I just forgot to put a little letter "i" on the line, also, I would like to show you my way to deal with the query result. First, the line as it should be:
$result3 = mysqli_query($con, $query3);
Notice the i after mysql. Now let us check whether we got some rows or not:
if (!$result3) {
die('Could not retrieve data: ' . mysqli_error($con));
} else {
while ($row = mysqli_fetch_array($result3)) {
//Show your results
}
}

Joining a resource and a table in php / mysql

I am relatively new to web development and I am trying to query a MySQL database with a database through the following commands, however I unable to do so and I am getting the following as error:
PHP Code:
$query1 = "SELECT id_2 FROM idTable WHERE id = '$idno'";
$result1 = mysql_query($query1);
if (!$result1)
die("Database access failed(error 7): " . mysql_error());
/************ possible error point *******************/
$query2 = "SELECT * FROM Data NATURAL JOIN $result1 LIMIT $num,$last_num";
$result = mysql_query($query2);
if (!$result)
die("Database access failed(error 8): " . mysql_error());
Error:
Database access failed(error 8): Table 'Database.Resource' doesn't exist
Basically I have two tables. I need to choose some values from the 'id_2' column of idTable and depending upon the values chosen, I want to pick all the rows from the table 'Data' that match the corresponding ids by performing a join operation. Can anybody please tell me how to achieve the join of the resource returned and the table (Or in general, how do I solve my problem)?
SELECT d.*
FROM Data d
JOIN idtable i
ON i.id2 = d.id
WHERE i.id = $idno
ORDER
BY d.id
LIMIT $num,$last_num;
I am still not sure what is relations between these 2 tables but valid code and query should be something like :
$query1 = "SELECT * FROM Data
INNER JOIN idtable
ON idtable.id = Data.id_2
AND idtable.id = $idno
LIMIT $num,$last_num";
$result = mysql_query($query1);
if (!$result)
die("Database access failed : " . mysql_error());
But valid is not equal correct or good :-)
And once I've reread my own query, I have the same question as #Strawberry: why do we need join? why not just:
$query1 = "SELECT * FROM Data
WHERE Data.id_2 = $idno
LIMIT $num,$last_num";
$result = mysql_query($query1);
if (!$result)
die("Database access failed : " . mysql_error());

AJAX Called PHP Query Not Working

I'm attempting to display dynamic data in a Bootstrap Modal using an AJAX call to a PHP query. The Javascript function is working fine and passing the ID to be used in the .php file, but the query itself doesn't seem to be working. I must be missing something pretty simple, but I don't get why it isn't working.
SQL / PHP
This file is being called and appears to be connecting to the db correctly, but the query itself isn't working.
$q = intval($_GET['q']);
$con = mysqli_connect('omitted','omitted','omitted','omitted');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT * FROM Orders WHERE orderID = '".$q."'";
$result = mysql_query($con, $sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
No result is being returned, and no error message is coming back. A var_dump of all the variables used in this query returns the following: (When the die conditional is removed)
var_dump($q) = int(3)
var_dump($sql) = string(40) "SELECT * FROM Orders WHERE orderID = '3'"
var_dump($result) = bool(false)
It has nothing to do with AJAX. You are mixing mysql and mysqli.
Try the following -
$sql = "SELECT * FROM Orders WHERE orderID = '".$q."'";
$result = mysqli_query($con, $sql);
if (!$result) {
die('Invalid query: ' . mysqli_error());
}

no db connection but no error

here is my db connection code and query code:
// Connecting, selecting database
$link = mysql_connect('MySQLA22.webcontrolcenter.com', 'shudson', '*******')
or die('Could not connect: ' . mysql_error());
mysql_select_db('henrybuilt') or die('Could not select database');
$sql = "SELECT ID, vcImageName FROM corp_images WHERE idPage = 6";
$query = mysql_query($sql) or die ("Error");
There is no 'or die' error upon connecting, selecting, or querying
There is no error_log file
The sql query executes if I run it in my sql browser
What is going on???
$sql = "SELECT ID, vcImageName FROM corp_images WHERE idPage = 6"
Its missing ;
$sql = "SELECT ID, vcImageName FROM corp_images WHERE idPage = 6";
It appears correct...try adding this adapted code from the manual to further debug and see what you learn:
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$query) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sql;
die($message);
}
// Use result
// Attempting to print $result won't allow access to information in the resource
// One of the mysql result functions must be used
// See also mysql_result(), mysql_fetch_array(), mysql_fetch_row(), etc.
while ($row = mysql_fetch_assoc($query)) {
echo $row['ID'];
echo $row['vcImageName'];
}

Can't get results from mySQL query to display in PHP

I am very new to PHP and can't seem to use mySQL data at all from PHP. The SQL query I wrote works fine in phpMyAdmin when I run it in the SQL editor, but the most I am able to get from the code is the following from var_dump. Nothing else displays anything at all.
resource(3) of type (mysql result)
Any help at all would be greatly appreciated!!
<?php
ini_set(‘display_errors’,1);
error_reporting(E_ALL|E_STRICT);
$con = mysql_connect("localhost","XXXXXXXX","XXXXXXX");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sharetrader", $con);
$sharelist = mysql_query("SELECT DISTINCT tblStocks.stockSymbol, tblShareData.lookupDate FROM tblStocks LEFT JOIN tblShareData ON tblShareData.tickerCode = tblStocks.stockSymbol ORDER BY tblShareData.lookupDate ASC LIMIT 0 , 30");
if (!$sharelist) {
die('Invalid query: ' . mysql_error());
}
var_dump($sharelist);
while ($row = mysql_fetch_array($sharelist)) {
echo $row['tblStocks.stockSymbol'];
}
mysql_close($con);
?>
I am very new to PHP
But you're making great progress - just missing some of the finer points.
try:
while ($row = mysql_fetch_array($sharelist)) {
var_dump($row);
}
...and it should be obvious what's happening.

Categories