We have 2 different databases that I'm trying query against each other with an inner join.
When I run the query from phpmyadmin, the query works perfectly. However, when I attempt to put the query into a php page, I cannot get the line to work. I assume I'm missing something in the mysql_select_db line where I reference the host/db/user/pass for the first database.
What am I missing here to get this query to function on the page? Again, I'm confident the actual query works since it does run in phpmyadmin.
Thanks in advance as always.
Here's the code I'm working with....
$hostname_db = "123.456.78.910";
$database_db = "votes_db";
$username_db = "votes_dbuser";
$password_db = "password123";
$db = mysql_connect($hostname_db, $username_db, $password_db, true) or trigger_error(mysql_error(),E_USER_ERROR);
$hostname_db2 = "123.456.78.910";
$database_db2 = "survey_db";
$username_db2 = "survey_dbuser";
$password_db2 = "password456";
$db2 = mysql_connect($hostname_db2, $username_db2, $password_db2, true);
// trying to make this work, query ok in phpmyadmin, but not on the php page
mysql_select_db($database_db, $db);
$query_testdb3 = sprintf("SELECT votes_db.vote_table.vote_survey_id
FROM votes_db.vote_table
inner join survey_db.survey_table
ON votes_db.vote_table.vote_survey_id = survey_db.survey_table.survey_id
WHERE votes_db.vote_table.vote_survey_id = 1457 ");
$testdb3 = mysql_query($query_testdb3, $db) or die(mysql_error());
$row_testdb3 = mysql_fetch_assoc($testdb3);
$totalRows_testdb3 = mysql_num_rows($testdb3);
Sometime ago I have the same issue (mysql-php multiple databases problem)
So, remove the "true" option on the second ($db2) and everything should be fine.
Gl
$`totalRows_testdb3 = mysql_num_rows($testdb3);`
change the column # in "$testdb3" in the last line to "$row_testdb3".
$totalRows_testdb3 = mysql_num_rows($row_testdb3);
Also if you put each $db and db2 in separate class and function with a:
mysql_close($db);
after each query.
It was a permissions problem. The user that was connecting to the database didn't have the proper privliges. Hosting company fixed this for me, so I can't tell you exactly what they did, but it now works.
Tim was correct, it was working from phpmyadmin since I was logged in as a superuser...
Related
I'm having a problem with the query it works fine in SQL server but when I trying to return the row in PHP it comes back without any results. See the code below the issue is with this field SO_SalesOrderHistoryDetail.ItemCodeDesc this field is exactly what it sounds like it's an item description and on this particular record it has this data in it. ACC, Smart-Trek™ Deluxe Stereo
I have confirmed this is the issue ™ for some reason this field will not display in PHP. I could edit the data although I would like to reserved that option for a last ditch effort. I'm not sure why this particular data is stopping all information from being returned. I don't get a PHP error. Any help would be most welcome much appreciated.
$tsql = "
SELECT
SO_SalesOrderHistoryHeader.CustomerNo,
SO_SalesOrderHistoryHeader.CustomerPONo,
SO_SalesOrderHistoryDetail.SalesOrderNo,
SO_SalesOrderHistoryDetail.CancelledLine,
SO_SalesOrderHistoryDetail.ItemCode,
SO_SalesOrderHistoryDetail.ItemCodeDesc,
SO_SalesOrderHistoryDetail.QuantityBackordered,
SO_SalesOrderHistoryDetail.QuantityOrderedRevised,
SO_SalesOrderHistoryDetail.PurchaseOrderNo,
SO_SalesOrderHistoryDetail.QuantityShipped,
SO_SalesOrderHistoryHeader.OrderStatus,
SO_SalesOrderHistoryHeader.OrderDate,
SO_SalesOrderHistoryHeader.CustomerPONo
FROM SO_SalesOrderHistoryDetail
INNER JOIN SO_SalesOrderHistoryHeader ON
SO_SalesOrderHistoryDetail.SalesOrderNo = SO_SalesOrderHistoryHeader.SalesOrderNo
WHERE
(SO_SalesOrderHistoryHeader.CustomerNo = 'XXXXXXX') AND
(SO_SalesOrderHistoryHeader.OrderStatus <> 'X') AND
(SO_SalesOrderHistoryDetail.CancelledLine = 'N') AND
(SO_SalesOrderHistoryHeader.CustomerPONo = 'XXXXXXX')
";
$getResults= sqlsrv_query($connms, $tsql);
$row = sqlsrv_fetch_array($getResults);
I found the answer I thought I'd pass it along to help others. It can be found at the URL below.
Add this for your connection configuration array: "CharacterSet" =>"UTF-8".
For example:
$connectionInfo = array( "Database"=>"DBName", "CharacterSet" =>"UTF-8" );
https://learn.microsoft.com/en-us/archive/blogs/brian_swan/sql-server-driver-for-php-connection-options-characterset
I am stuck on a query/calculation based on two different tables (joined by a third table). A form collects information to input into tbl2, tbl1 is linked to tbl2 via foreign key. I need to calculate and insert a value into tbl2 based on form data and a value from tbl1.
My Query + Calculation looks like this:
$res = $user->runQuery("SELECT binWeight FROM jobBins jb JOIN butchJobOpen bjo ON bjo.jobBins_binID = jb.binID JOIN butchJobClose bjc ON bjc.butchJobOpen_butchJobNum = bjo.butchJobNum WHERE bjo.butchJobNum = '$butchJobOpen_butchJobNum'");
$res->execute();
$row = $res->fetch(PDO::FETCH_ASSOC);
//Calculate $trimYield
$trimYield = $row['binWeight'] / $trimWeight;
The query should use a form value $butchJobOpen_butchJobNum, verify the bin exists on the join conditions, and return the weight of the bin....this works fine as a standalone query, however, I cannot get any results from my calculation...I am sure I am just missing something stupid...but I am stuck, so I lob this up to the merciless SO community, in hopes that while getting downvoted, someone will point me in the right direction while ridiculing my obvious mistake.
Okay, here is what you have to do
Get rid of all the home-brewed stuff. Instead of whatever $user->runQuery use vanilla PDO.
Verify the input. See whether your variable contain anything useful.
Use PDO properly, utilizing prepared statements.
Make your code to give at least any outcome.
Do realize that above instruction is not a ready to use solution on a golden plate but just steps you have to make yourself to investigate the problem.
So
ini_set('display_errors',1);
error_reporting(E_ALL);
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
$pdo = new PDO($dsn, $user, $pass, $opt);
var_dump("that argle-bargle var name I can't type it:",$butchJobOpen_butchJobNum);
$sql = "SELECT binWeight FROM jobBins jb
JOIN butchJobOpen bjo ON bjo.jobBins_binID = jb.binID
JOIN butchJobClose bjc ON bjc.butchJobOpen_butchJobNum = bjo.butchJobNum
WHERE bjo.butchJobNum = ?";
$res = $pdo->prepare($sql);
$res->execute([$butchJobOpen_butchJobNum]);
$binWeight = $res->fetchColumn();
var_dump("bin weight:", $binWeight);
var_dump("trim weight:", $trimWeight);
$trimYield = $binWeight / $trimWeight;
var_dump("finally", $trimYield);
The var_dump part is most essential, giving you a clue what's going on in your program. A 10000 times more fruitful than a hour of staring at the code or a whine on SO.
The point here is to verify every bloody variable's state. And yo see whether it contains anything expected.
i need help. I have a query:
include_once("php_includes/db_conx.php");
......
$m = $_GET['m'];
$sql = "SELECT CID,name from course WHERE major='$m'";
$query = mysqli_query($db_conx,$sql);
if(mysqli_num_rows($query)!=0){..... etc
where $m is a valid variable, and db_conx.php is code for connecting to database. But my $query is returning false, i checked my query seperately in my php my admin, so it is valid. I suspect that it is due to that somehow i am not able to connec to the database. However this works in localhost, and don't on a real server.
Thanks in advance.
I'm trying to create a variable which is dependent on some information from the database. I'm trying to generate a $path variable which stores a path, depending on what information is recovered from the database.
$linkid = mysql_connect('localhost','user','password');
mysql_select_db("table", $linkid);
$variable = "00001";
$groupID = null;
$temp = mysql_query("SELECT groupID FROM table WHERE memberID='$variable'", $linkid);
while ($row = mysql_fetch_row($temp)){
global $groupID;
foreach ($row as $field){
$groupID = $field;
}
}
....
$path = "C:\WAMP\www\project\\" . $groupID;
$dir_handle = #opendir($path) or die('Unable to open $path');
The idea behind this is that $variable is set before the PHP is run, however it's set to 00001 for testing. The ideal situation is that $path should equal C:\WAMP\www\project\00001\. Currently, when I echo back the $path all I get is the original path without the $groupID added to the end.
I also receive the message "mysql_fetch_row() expects parameter 1 to be resource" but I've used this method for retrieving information before and it worked just fine, and I set up my table in the same way so I don't think the issue is there.
I have a feeling I'm missing something obvious, so any help is appreciated. It's not for an assignment or anything school related (just trying stuff out to learn more) so knock yourselves out with correcting it and explaining why :)
In addition, only one memberID will ever be a match to the $variable, so if there's an alternative way to fetch it I'd appreciate knowing.
Oh, and I know my variable names are shocking but they're only that on here, on my actual code they're different so no criticism please :p
EDIT: The SQL query is correct, after following BT634's advice and when running it on phpMyAdmin I get the groupID I want and expect.
mysql_select_db("table", $linkid)
should actually be
mysql_select_db("database_name", $linkid)
since you are connecting to the database that contains the table and not the table itself.
Also, try mysql_result($temp,0) instead of the while loop
First of all, you're not specifying what database to connect to in your connection - you're specifying what table. You might also want to check how many rows your query is returning:
$temp = mysql_query("SELECT groupID FROM table WHERE memberID='$variable'", $linkid);
echo mysql_num_rows($temp);
If it's still complaining about $temp not being a valid resource, change your MySQL connection code to:
// Establish connection
$con = mysql_connect("localhost","peter","abc123");
if (!$con) die('Could not connect: ' . mysql_error());
mysql_select_db("my_db", $con);
// Make your query
$result = mysql_query("SELECT groupID FROM table WHERE memberID='$variable'");
// Find out what the value of the query is (i.e. what object/resource it is)
var_dump($result);
Once you know that MySQL is returning valid data, extract the values you want. You don't have to use globals:
while ($row = mysql_fetch_row($temp)){
$groupId = $row[0];
}
// Use $groupId however you please...
One thing to bear in mind is that mysql_fetch_row will return
array
(
0 => '...'
)
Whilst mysql_fetch_assoc will return:
array
(
'groupId' => '...'
)
Find out what query it's definitely running, and paste that into a normal MySQL client to make sure your query is correct.
Just do this after defining "$variable"
exit("SELECT groupID FROM table WHERE memberID='$variable'");
Then copy the output into a MySQL client (or MySQL from the command line).
Try something like this:
global $groupID;
$linkid = mysql_connect('localhost','user','password');
mysql_select_db("table", $linkid);
$variable = "00001";
$groupID = null;
$sql = "SELECT groupID FROM table WHERE memberID='$variable'";
$temp = mysql_query($sql, $linkid) or die(mysql_error());
$row = mysql_fetch_row($temp);
if ($row) {
$groupID = $row['groupID'];
}
If you are retrieving a single value, and it is guaranteed to be unique, then the loop structures are unnecessary. I've added a check to ensure the query exits with an error if there's a problem - it is ideal to do this everywhere, so for example do it with mysql_select_db too.
Hi Guys my code is below.
What I'm trying to do is to update all of the value in mysql that satisfy a specific a specific condition. Now when I just type in the mysql query:
UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'
The query works fine but I can't seem to write php code that will do this.
Any help is welcomed
<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
// Part 2 (works fine)
$test2= "SELECT * FROM `video`";
$results= mysql_query($test2, $connect);
$num_rows = mysql_num_rows($results);
// End of part 2
// Part 3 ( Not too sure about this part)
for ($num_rows= $count; $count >= 0; $count--)
{
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'",$connect);
}
// End of part 3 ( Not too sure about this part)
?>
<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
//no need of part 2 to update records in table
//part 3 loop removed
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0' ",$connect);
?>
Try this. May it will solve ur problem.
For starters I'd take the query out of the loop (and just delete the loop). If it still doesn't work, try hard coding the database name. If that doesn't work, make sure $connect is what you think it is. Look up mysql_query() and make sure you're using it right.
If you try everything above, you'll either find the problem or at least figure out what the problem is not.