Mysql - Select WHERE AND - VARIABLE does not work - php

I have the following problem:
//$sql = "SELECT name, number, letter FROM queues WHERE number='100' AND letter='S'";
$sql = "SELECT name, number, letter FROM queues WHERE number='".$contentnumber."' AND
letter='".$contentletter."'";
See both queries above.
Both give the following out when I execute: var_dump($sql);
string(121) "SELECT name, number, letter FROM queues WHERE number='100' AND letter='S'"
Output by both is the same but with the version of the variables data from the database is not loaded. But with the version where I put the values for numbers and letters, it works fine.
Does anyone know where the problem or the mistake is?
Here is a little bit detailed:
ob_start();
echo "{{tokens[0].token_letter}}";
$contentletter = ob_get_clean();
ob_start();
echo "{{tokens[0].token_number}}";
$contentnumber = ob_get_clean();
$conn = mysqli_connect($serverName, $userName, $password, $dbName);
if (!$conn) { die("Connection failed: " . mysqli_connect_error()); }
$sql = "SELECT name, number, letter FROM queues WHERE
number='".$contentnumber."' AND letter='".$contentletter."'";
var_dump($sql);

Related

Get specific information from a database to display on webiste

I have got it to call all information I want to show on the website but I actually only need certain data showing such as only things with project id of 1.
I've tried a few different while functions.
//creating connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, user_id, project_id, hours FROM hours_worked";
$result = $conn->query($sql);
Just change your sql query to:
$sql = "SELECT id, user_id, project_id, hours FROM hours_worked WHERE project_id ='".$project_id."';
Change the WHERE clause according to your need.
If you want to display just the records associated with a particular user, then change the WHERE clause to:
WHERE user_id = *ENTER USER ID HERE*

Updated a MySQL table with PHP

This seems so easy. My code checks out, and even if I run the resetting fuction in phpMyAdmin it works, but for some reason the table never updates when I run the php.
<?php
$servername="localhost" ;
$username="***" ;
$password="***" ;
$dbname="***" ;
$conn=new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT `Alert` FROM `poonpad_daveAlert` WHERE 1";
$result = $conn->query($sql);
if ($result == 1) {
echo "wow, stuff is happening. resetting."; resetting();
} else {
echo "this place is dead";
}
function resetting() {
$sql = "UPDATE `poonpad_daveAlert` SET `Alert`=2 WHERE 1";
}
$conn->close();
?>
First, I'd use the below to execute the query.
$result = mysqli_query($conn,$sql);
Second, instead of (result==1) which I'm not sure is intended for what purpose, you should use (mysqli_num_rows($result)>0).
Third, for the resetting function, I think you need to mention the column name for the WHERE condition which you set to 1 as well as the main $sql query, and you don't need the WHERE if you have to select all rows.
"UPDATE `poonpad_daveAlert` SET `Alert`=2 WHERE [column-name]=1";

Issue with JOIN in PHP MySQL

Having a bit of a struggle here with adding JOINs to a query. I am connecting to two separate databases (on the same server). For this reason, I am writing this mysqli simply and will convert to a prepared statement once it's working.
// REMOVED: DB VARIABLES
$conn = new mysqli($servername, $username, $password, $db_connective_data);
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }
$conn2 = new mysqli($servername, $username, $password, $db_resources);
if ($conn2->connect_error) { die("Connection failed: " . $conn2->connect_error); }
$sql = "SELECT * FROM downloads LEFT JOIN resource_data ON downloads.resource_id_REF=resource_data.resource_id WHERE downloads.user_basics_id_REF='$user_id'";
$result = $conn->query($sql);
$number_of_download_rows_returned = mysqli_num_rows($result) -1;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$resource_id_REF[] = $row['resource_id_REF'];
$download_date[] = date('Y-m-d', strtotime($row['download_date']));
$resource_title[] = $row['resource_title'];
$resource_title_link[] = str_replace(" ", "-", $row['resource_title']);
}
}
$conn->close();
A query without a JOIN works fine (albeit without returning the resource_title):
$sql = "SELECT * FROM downloads WHERE downloads.user_basics_id_REF='$user_id' ORDER BY downloads.download_date DESC";
What am I missing here? The first code sample will return no results. The second one will return three.
Any assistance is greatly appreciated.
Here is a list of the different database names that I reference. As I stated, some data is in the "connective_data" db and some is in the "resources" db.
$db_connective_data = "connective_data";
$db_lists = "lists";
$db_messaging = "messaging";
$db_resources = "resources";
$db_users = "users";
I can't seem to get two of them connected. Am I missing something strikingly obvious here?
There is no need to create 2 connections if the databases are located on the same mysql server. You can simply reference tables from another database as databasename.tablename.
As a result, you can join 2 tables from 2 different databases as:
$sql = "SELECT * FROM yourdatabase1.downloads LEFT JOIN yourdatabase2.resource_data ON yourdatabase1.downloads.resource_id_REF=yourdatabase2.resource_data.resource_id WHERE yourdatabase1.downloads.user_basics_id_REF='$user_id'";
Obviously, you need to substitute your real database names for yourdatabase1 and yourdatabase2 in the above query.
Update: Are you sure you need so many databases? These seem to be tables to me, not databases.

Simple and effective way to echo the result of a query in PHP?

I'm new to MySQL and PHP and I m struggling to echo my queries (the results not the text!)
I have searched for this but nothing seems to work properly, the best I managed to do was echoing the text of the query. I might have some fatal mistakes but here is my code:
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("atoma",$dbhandle)
or die("Could not select atoma");
$sql1 = mysql_query("SELECT (MAX(GRADE)) as maximum FROM thema3");
$r = mysql_fetch_array($sql1); // will return the result
echo $r['maximum'];
$sql2 = "SELECT COUNT(DISTINCT NAME) FROM thema2";
$sql1 = "SELECT AVG(GRADE) FROM thema3";
mysql_close($dbhandle);
?>
I get nothing as a result.
I have these 3 queries and all I want is just to print their results. I've written code for echoing only one of the 3 since the other 2 will be echoed as the first one I want to believe.
Your code seems incorrect because, the connection is mysqli and fetching is using mysql
$conn = new mysqli($servername, $username, $password, $dbname);
....
$sql1 = mysql_query("SELECT (MAX(GRADE)) as maximum FROM thema3");
$r = mysql_fetch_array($sql1); // will return the result
A full example of W3Schools
<?php
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
http://www.w3schools.com/php/php_mysql_select.asp
When you use max, avg, etc you pull only one result, so with the $result[0] you has the result you want
Edit:
If you're new, maybe you come to see read this:
http://codular.com/php-mysqli
So A) would leave using an outdated way to call the database, and B) with this in principle bringing the first row you would have the result of AVG, MAX, etc. when only one row which returns you if you make this types of sentences ;)

Mysqli Update not working properly

$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "UPDATE Cube SET xValue=15 WHERE Index=1";
mysqli_query($conn, $sql);
mysqli_close($conn);
This seems like it should be pretty straight forward, but for some reason, the xValue field won't change, and I get no errors whatsoever. Been trying this for too long.
as u_mulder said, index is a reserved word in mySQL so you got 2 options:
$sql = "UPDATE Cube SET xValue=15 WHERE Cube.Index=1";
or
$sql = "UPDATE Cube SET xValue=15 WHERE `Index`=1";

Categories