MySQL Database Access [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
NOTE: Question Closed. I managed to figure out what was wrong.
My current code is this:
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_select_db($con, "rbxdataa_Data");
$Amount=$_GET["Amount"];
$GetType=$_GET["Type"];
$sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_run = mysqli_query($con, $sql);
while($sql_row = mysqli_fetch_assoc($sql_run)){
echo $sql_row['EventId'].'<br>';
}
mysqli_close($con);
?>
As I am currently very new to PHP and MySQL, I have no idea why this code will not work. I am also confused as to how I would make it echo only the top ($Amount) as determined by how large the "EventId" value is.
My intent is to gather the [$Amount] highest rows in the table with the EventType $GetType.
I am aware of SQL Injection vulnerability, however for my purposes this does not affect me.
Error:
"Parse error: syntax error, unexpected T_STRING on line 4"
This isn't the main error, however. I just can't read the main error yet.

In your line 4, mysqli_connect_error() doesn't exist. Please check mysqli documentation http://www.php.net/manual/en/book.mysqli.php
Edit: You may try: printf("Connect failed: %s\n", mysqli_error($con));

Please use this code:
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
mysqli_select_db($con, "rbxdataa_Data");
$Amount=$_GET["Amount"];
$GetType=$_GET["Type"];
$sql = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_run = mysqli_query($con, $sql);
while($sql_row = mysqli_fetch_assoc($sql_run)){
echo $sql_row['EventId'].'<br>';
}
mysqli_close($con);
?>

Try this.
$con=mysqli_connect("HOST","USERNAME","PASSWORD","rbxdataa_Data");
$sql_query = "SELECT * FROM EventRecord WHERE EventType='$GetType' ORDER BY EventId DESC";
$sql_result = mysqli_query($con,$sql_query)
or exit("Query fail message");
Otherwise looks like what I'm used to.
I just realized that no one has even asked about the $_GET variable you're using you're using. You set $GetType from $_GET['Type']. It could be that.

Related

Fetching data from a temporary table [closed]

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'];
}

PHP/SQL connection script [closed]

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);

PHP API That fetches and displays a row form sql database [closed]

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
I have been trying to code an API that when a button is pressed on a program is fetches and displays a random row from a mysql database in this format - Text:Text - My code so far doesn't seem to work? Have a look:
<?php
// Create connection
$con=mysqli_connect("example.com","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysql_query("SELECT * FROM `data_submit` ORDER BY RAND() LIMIT 0,4;");
$row = mysql_fetch_array($result);
echo $row['name'];
mysqli_close($con);
?>
Please help!!! It's really annoying.
Thanks
You're mixing mysql_* and mysqli_* extensions. You can't use them interchangeably, you must use one or the other (since mysql_* is deprecated, as per my comment above, you should consider using MySQLi). You also must loop over the result, since you're returning an array:
<?php
// Create connection
$con=mysqli_connect("example.com","user","pass","database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM `data_submit` ORDER BY RAND() LIMIT 0,4;");
while ($row = mysqli_fetch_array($result)) {
echo $row['name'];
}
mysqli_close($con);
?>
You start with a mysqli_connect but switch to the mysql_query (the non-improved version). You should stick to the mysqli-library: mysqli_query and mysqli_fetch_array
Sidenote your current query will be very slow on larger tables, here is a faster one.
SELECT *
FROM table
WHERE id >= ( SELECT FLOOR( MAX(id) * RAND()) FROM table )
ORDER BY id LIMIT 4

Simple code not working [closed]

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
Hello guys I want to show the database on my website using this code which is given below but it's giving me annoying error again and again. I have tried everything but nothing is working it give me same error notice
Here is the error Notice
Notice: Undefined index: name in C:\xampp\htdocs\test3\index.php on line 15
Here is the PHP Code
<?php
$connect = mysql_connect("localhost","root","123");
if(!$connect) {
die("Failed to Connect: " . mysql_error());
}
if (!mysql_select_db ("login")){
die("Failed to Select DB: ". mysql_error());
}
$results = mysql_query ("Select * from users ");
while($row = mysql_fetch_array($results)){
echo $row['name'];
}
?>
I have also tried to replace mysql_fetch_array($results) with this mysqli_fetch_assoc($result) and it's also not working please run this code yourself and then give me that code. Thanks
you can use var_dump() to check the result
while($row = mysql_fetch_array($results)){
var_dump($row);
}
check the output, whether field name exists
Your database "login", table "users" doesn't have a column "name". That could be because of a typo with CaSe sEnsitivitY or other just missing.
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("localhost", "root", "abcd")or die("cannot connect");
mysql_select_db("testDB")or die("cannot select DB");
$sql="SELECT * FROM login WHERE userid='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
//you can then mysql_fetch_array or mysql_fetch_ob
...
?>
this will surely help you
You can also simply do it this way to avoid confusion on the if's
mysql_connect("localhost","root","123") or die("Failed to Connect");
mysql_select_db ("login") or die ("cannot connect to db");
$results = mysql_query ("Select * from users ");
while($row = mysql_fetch_assoc($results))
{
echo $row['name'];
}
Since the error refers to the name, you might want to check your users table if the column name exists.

MySQL table does not update [closed]

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())

Categories