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);
Related
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 1 year ago.
Improve this question
I need PHP code to detect is that line in MySQL database table.
Thanks
from my understanding of your question - in your php code you can do something like this
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
$sqlSearch = "select everything from <table_name> where <column_name> = <value>;";
$result = mysqli_query($con,$sqlSearch );
if (mysqli_num_rows($result) > 0){
// do nothing
}
else{
$sqlInsert = "insert into <table_name> (column1,column2...) VALUES(value1,value2...);";
$result = mysqli_query($con,$sqlInsert);
}
?>
basically, you want to search the database to see if that row exist and if it does not you want to perform an insert query.
I hope this piece of code, helps you.
<?php
$variable1 = 1;
$query = "SELECT * FROM table_name WHERE column_name='$variable1'";
$result = $conn->query($query); //$conn is the connection variable
if($result->num_rows==0){
//insert into table if no duplicate tuple doesnot exist.
$insertquery = "INSERT INTO table_name (column_name) VALUES ('$variable1')";
$insertresult = $conn->query($insertquery);
}else{
//do something
}
?>
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'];
}
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 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 6 years ago.
Improve this question
here's a part of my code that AFTER you submit your login credentials checks your username, password, etc...:
mysql_select_db("robur_mike") or die ("Could not find DB!");
$query = mysql_query("SELECT * FROM Bx1_Users WHERE Username='$username'");
$numrows = mysql_num_rows($query);
if ($numrows!=0)
{
while ($row =mysql_fetch_assoc($query))
{
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
.....
I now need to "translate" that to run under a DB2 database in BlueMix. I am already connected to the database using the code provided here:
How to connect to a SQL Database-s2 from a .php application in BlueMix
The query should be OK since it is basic SQL. What you should change is the way you run it, since in your old code you are using the mysql library.
Looking at the other question, I assume that you are able to connect doing something like:
$conn = db2_connect($conn_string, '', '');
Now to execute the query you can use db2_exec 'translating' your code to something like:
$sql = "SELECT * FROM <schemaName>.Bx1_Users WHERE Username='$username'";
if ($conn) {
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
while ($row = db2_fetch_assoc($stmt)) {
$dbusername = $row['Username'];
$dbpassword = $row['Password'];
$dbfirstname = $row['FirstName'];
$dblastname = $row['LastName'];
}
}
db2_close($conn);
As you can see I've added a placeholder for the schema name in the SQL query. You can retrieve it within your SQL Database dashboard (Manage/Work with tables).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is my code
I have 2 table
booking // create to keep the booking from member
userinfo //keep username of member
<?php
$ta = $_POST['table'];
$day=$_POST['date'];
$usern = $_SESSION['username'];
$con = mysql_connect("localhost","root","password") or die ("can't connect to host");
mysql_select_db("register",$con) or die("can't connect to database");
$sql = "select * from userinfo where username = '$usern' ";
$rs = mysql_query($sql);
$row = mysql_num_rows($rs);
if($row == 0)
{ mysql_query("insert into booking(username) values '".$usern. "' ");
mysql_db_query("register");
} mysql_close();
?>
i don't know why it doesn't update.
You're missing your parenthesis in your INSERT statement:
"insert into booking(username) values '".$usern. "' "
should be
"insert into booking(username) values ('".$usern. "') "
First things first: mysql is becoming unsupported. Use Mysqli instead. It's not much different...
Then, the issue you have is to leave out the "_db". It's just mysql_query(your request). Plus, the second statement doesn't need to exist.
Need error handling? Include (before ;) "or die (mysql_error())