Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am not able to fetch data from a temporary table that I have just created with a SELECT statement off another table.
$link = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();}
$query = "create temporary table temp1 select name from trn_games";
$query2 = "select name from temp1";
$result = mysqli_query($link, $query2) or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
Note: To create a table by SQL Query you should execute it by mysqli_query($link, $query);.
You only write SQL Query but do not execute it.
$query = "create temporary table temp1 select name from trn_games";
$execute = mysqli_query($link, $query); // here execute your SQL QUERY.
$query2 = "select name from temp1";
$result = mysqli_query($link, $query2);
while($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a string called $searchquery. $searchquery contains this SQL command:
SELECT * FROM `videos` WHERE (`Title` LIKE "%query%" OR `Tags` LIKE "%query%")
How can I make MySQL execute this command from a PHP page then display the results on PHP page??
I have tried
$sql = ($searchquery)
But dosent seem to work?
Firstly you need to create database connection
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// then execute your query
$sql = "SELECT * FROM `videos` WHERE (`Title` LIKE '%query%' OR `Tags` LIKE '%query%')";
$result = mysqli_query($conn, $sql);
// and fetch result set
while($row = mysqli_fetch_assoc($result)) {
print_r($row);
}
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I try to learn mysql an have a problem with a sql query.
i made a database connection with mysqli
the php script create a table code_scanned in database.
Creating the Table works very good!
But my second query to fill the table wont work :(
If i paste the query manually to phpadmin it works.
But not in my php script.
Can please anyone have a look on my code, what i do wrong.
$servername = "xxxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// sql to create table
$sql = "CREATE TABLE IF NOT EXISTS code_scanned (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
produkt VARCHAR(50) NOT NULL,
code VARCHAR(30) NOT NULL
)";
if ($conn->query($sql) === TRUE) {
echo "code_scanned erstellt";
} else {
echo "Fehler" . $conn->error;
}
$sql = "INSERT INTO code_scanned (produkt, code) VALUES ('gates', 'Microsoft')";
$conn->close();
Exexute the insert query :
And correct The code:
$sql = "INSERT INTO code_scanned (produkt, code) VALUES ('gates', 'Microsoft')";
$result= mysqli_query($conn,$sql);
if($result)
{
echo "Insert sucessfully";
}
else
{
echo("Sorry:".mysqli_errno($conn));
}
$conn->close();
You are not running the second query. Put this below the line that has $sql = "INSERT...."
$conn->query($sql);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hello there im new in php, im trying to make a news plugin on my website. But i have following error which i cant get fixed.
$query = "SELECT `id` , `headline`, `timestamp` FROM `news` ORDER BY `timestamp` DESC";
$result = #mysql_query($query);
if(!$result){
echo('Error selecting news: ' . mysql_error());
exit();
}
if (mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_object($result))
{
?>
<font size="-1"><b><? echo $row[headling]; ?></b> <i><? echo formatDate($row[timestamp]); ?></i></font><?
}
}
mysql_close($conn);
?>
Im geting same error over and over again.
Error: Error selecting news: No database selected
you need to select a database.
see mysql_select_db http://php.net/manual/de/function.mysql-select-db.php
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Connection failed : ' . mysql_error());
}
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Db does not exist : ' . mysql_error());
}
?>
Also note that mysql_* is deprecated. use new php style like
http://php.net/manual/de/mysqli.select-db.php
Database in this example is "test"
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
/* change db to world db */
$mysqli->select_db("world");
/* return name of current default database */
if ($result = $mysqli->query("SELECT DATABASE()")) {
$row = $result->fetch_row();
printf("Default database is %s.\n", $row[0]);
$result->close();
}
$mysqli->close();
?>
I'm guessing you need to specify which database you're attempting to query in your statement.
select a.id , a.headline, a.timestamp
from database.news a
order by a.timestamp desc
Replace "database" above with the name of yours.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
someone please help me to set mysql database connection variable on below php scripts but I couldn't fixed it.
$sql = "SELECT DISTINCT r.itemID, r2.ratingValue - r.ratingValue
as rating_difference
FROM rating r, rating r2
WHERE r.userID=$userID AND
r2.itemID=$itemID AND
r2.userID=$userID;";
$db_result = mysql_query($sql, $connection);
$num_rows = mysql_num_rows($db_result);
while ($row = mysql_fetch_assoc($db_result)) {
$other_itemID = $row["itemID"];
$rating_difference = $row["rating_difference"];
if (mysql_num_rows(mysql_query("SELECT itemID1
FROM dev WHERE itemID1=$itemID AND itemID2=$other_itemID",
$connection)) > 0) {
$sql = "UPDATE dev SET count=count+1,
sum=sum+$rating_difference WHERE itemID1=$itemID
AND itemID2=$other_itemID";
mysql_query($sql, $connection);
if ($itemID != $other_itemID) {
$sql = "UPDATE dev SET count=count+1,
sum=sum-$rating_difference
WHERE (itemID1=$other_itemID AND itemID2=$itemID)";
mysql_query($sql, $connection);
}
}
You didn't connect to any database use some thing like this
$link = mysql_connect('dbhost', 'dbusername', 'dbpassword');
$connection = mysql_select_db('tablename', $link);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have a database which I'm querying: "select email from users where access=1". The error I'm receiving is
Fatal error: Call to a member function query() on a non-object on line: $result = $mysqli->query($query);
I'm using http://uk3.php.net/manual/en/mysqli.query.php as a reference and I can't see where I'm querying a mixture of Procedural and Object oriented PHP.
<?php
$db_host = "";
$db_user = "";
$db_pass = "";
$db_name = "";
/* OOP MYSQLI DATABASE CONNECTION */
$db = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
/*LOGIN QUERY */
$query = "select email from users where access=1";
$result = $mysqli->query($query);
$email= $result->fetch_array(MYSQLI_NUM);
if(isset($email[0])){
$query1="select id from user where email='".$email[0]."' and facebook=1";
$result1 = $mysqli->query($query1);
$email1= $result1->fetch_array(MYSQLI_NUM);
$userId=$admin[0];
}
?>
Would someone mind explaining please?
Your MySQLi object is not named $mysqli, its named $db
$result = $mysqli->query($query);
Should be
$result = $db->query($query);
Same goes for
if ($mysqli->connect_error) {
Should be
if ($db->connect_error) {
Edit
Just a side note, your second query makes no sense at all :) You just fetched an email from that table and then you are querying the same table again for that email?
$email= $result->fetch_array(MYSQLI_NUM);
if(isset($email[0])){
$query1="select id from user where email='".$email[0]."' and facebook=1";
All that setup is equivalent to your first query being
select id,email from users where access=1 and facebook=1
And you don't need that second query then