I want to select a value from the database with variables I get from $_GET, but it doesn't show any results. Could any one help me find what is wrong with my code?
here is my first page how i get the value to GET:
<?php
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
?>
<tr>
<td><?= $row['subject'] ?></td>
<td><?= $row['location'] ?></td>
<td><?= $row['geo'] ?></td>
<td><?= $row['date'] ?></td>
<td><?= $row['piority'] ?></td>
</tr>
<?php
}
}
?>
and here is my second page that i want to get data from database with subject variable from first page:
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '.$varPage.'");
while($row = mysqli_fetch_array($result))
?>
and now i want to write the date here is my code:
<p style="font-family:B Zar; direction:rtl; font-size:165%;"> <?= $row['date'] ?> </p>
but it writes nothing.
Is there anything wrong with my code?
There seems to be an oversight on your part with regards to your SQL. You have an extra-dot, which likely was a Typo. Try addressing that part and your code would most likely work as you expected. Some Guys here have suggested working with Prepared Statement (which is quite necessary) in which case this Snippet would use PDO as opposed to MySQLi... although the principles are not so extremely different.
<?php
$varPage = $_GET['subject'];
$servername = "localhost";
$username = "bayansh_user";
$password = "u)nHf,Accmo)";
$dbname = "bayansh_bmc";
$dbh = null;
// USING PDO INSTEAD::
$dsn = 'mysql:host=' . $servername . ';dbname=' . $dbname;
try {
$dbh = new PDO($dsn, $username, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
throw new Exception($e->getMessage());
}
// USING PREPARED STATEMENT... NOTICE "=:SBJ"
$sql = "SELECT `date` FROM `editor` WHERE subject=:SBJ";
// USING PREPARED STATEMENT CONTINUED: NOTICE "$dbh->prepare()"
$stmt = $dbh->prepare($sql);
// PASSING VALUES: NOTICE "['SBJ'=>$varPage]"
$stmt->execute( array('SBJ'=>$varPage) );
$resultSet = $stmt->fetchAll(PDO::FETCH_OBJ);
// LOOP THROUGH THE RESULT-SET AND DO SOME THINGS WITH THE DATA...
foreach($resultSet as $intKey=>$objData){
var_dump($objData->date); //<== DUMP SOME DATA TO THE STREAM
}
You should use the following code
$result = mysqli_query($conn,"SELECT `date` FROM `editor` WHERE subject = '".$varPage."'");
Related
I'm trying to echo the query made to my database. So far the code runs but nothing happens. I'm very new at coding so help or an explanation about what I'm doing wrong would be awesome! Thanks a lot!!!
<?php
$servername = "servername";
// REPLACE with your Database name
$dbname = "dbsname";
// REPLACE with Database user
$username = "username";
// REPLACE with Database user password
$password = "password";
$ID = 1;
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
echo "Error de conexion.";
}
$sql = "SELECT * FROM registro WHERE ID = '1' ";
$result = $conn->query($sql);
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
}
echo $data;
$conn->close();
Thanks everyone who replied, #AbraCadaver was right, I can't echo an array so I used print_r() instead and printed the variable $data. Now the php file prints the array with the query results. Thank you all!
$sql = "SELECT * FROM registro WHERE ID = $ID";
while ($data = $result->fetch_assoc()){
$sensor_data[] = $data;
print_r($data);
}
I am trying to figure out a way to have one PHP page to display all of my blog post but have the URL decide what post is requested from that database. Something kind of like this: localhost/bolg/posts.php?pid=1 In my database I have it set up to where each post has an ID associated with it. So what I want is something that put the pid=1 and put it in the MySQL code. Here is the PHP code of the post.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, title, content, date FROM posts where id =3";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Assuming you enter example.com?pid=10 in the browser address bar, you can capture that variable pid using the $_GET (docs) array which PHP automatically fills for you when a page is called with a querystring.
Using your existing code as a start point you can
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
if (isset($_GET['pid'])) {
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT id, title, content, date FROM posts where id = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param('i', $_GET['pid']);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// output data of each row
// while looop is not necessary, you are only returning one row
$row = $result->fetch_assoc();
echo "<h1> ". $row["title"]. "</h1>". $row["content"]. "" . $row["date"] . "<br>";
}
$conn->close();
} else {
echo "0 results";
}
Notice I took the liberty of amending your database access code to use prepared and parameterised query and binding the values to avoid SQL Injection Attack. You should always use this technique in the future
I'm doing a code where the main page has titles of some exercises: 'Exercises.php' (stored in a Mysql database) and depending on what title the user clicks (with links), I want the title and the question itself in another page: ‘question.php’. The questions will also be taken from the database. Im trying to use a GET parameter in the exercise link with the id of the exercise. Then in ‘question.php’, get the exercise with that id from the database.
This is some of the code that I've done so far but I'm stuck. Could you help me?
Thank you.
Exercises.php – In here I have all of the titles of the exercises displayed.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises";
$result = $conn->query($sql);
?>
<?php
while($row = $result->fetch_assoc())
{
?>
<tr>
<td><?php echo $row["exercise_id"]; ?></td>
<td><a name="search" href="http://localhost/PHP%20Pages/2.php" target="_blank"><?php echo $row["title"]; ?></a></td>
<td><?php echo $row["difficulty"]; ?></td>
</tr>
<?php
}
?>
question.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises"; /*Select from table name: exercises*/
$result = $conn->query($sql); /*Check connection*/
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo $row["exercise_id"] . ". " . $row["title"] . $row["text"] . "<br>";
}
}
?>
If you want to get the excericise_id from excercise.php you can change your href like this
Add Topic</td>
And you can get that exercise id in your question.php page like this
$id=$_GET['id'];
I would like you to suggest you to keep your db connection in separate file and try to include and use. that is not the good way to write database connection in every page.
I'm using this code here
<?php
error_reporting(1);
$servername = '127.0.0.1';
$username = '';
$password = '';
$dbname = 'splafpoo_users';
$conn = new mysqli($servername, $username, $password, $dbname);
if (mysqli_connect_errno()){
printf("<b>Connection failed:</b> %s\n", mysqli_connect_error());
exit;
}
$key = '';
if(isset($_POST['key'])){
$key = $_POST['key'];
}
$query = "SELECT * FROM users WHERE serial='$key'";
echo $query;
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row;
?>
Running the query SELECT * FROM users WHERE serial='test' in phpMyAdmin returns the desired result however when trying to display the result using the code above nothing is displayed and I cannot figure out how. How do I display the result?
You're gonna need a good old fashion while loop
while($row = $result->fetch_assoc()) {
echo $row['WHATEVERCOLUMNITISYOUWANT'];
}
also this is most definitely a duplicate.
Use var_dump($row) instead of echo $row or you use echo with a key:e.g. echo $row["user"]
I cant seem to get this code to work. It keeps giving me the error "sorry: query failed". There is connection to the database cause if i input a wrong password it will give the error "mysql connection failed". The table pet is already created and populated with data. Please I need help
<?php
$conn = mysql_connect ("localhost", "vistor", "visitor", "test")
or die ("sorry: Mysql connection failed");
$query = "select * from pet";
$result = mysql_query ($query,$conn)
or die ("sorry: query failed");
while($row = mysql_fetch_array($result)) {
echo $row['code'], " ", $row['name'];
echo "<br>";
}
mysql_close($conn);
?>
Donny gave me the below code and i added some css to make it appear as a table but i am suspecting i have the coding wrong as I am getting errors. thanks
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
echo "<table border = '2'>"
<tr>
<th>id</th>
<th>name</th>
</tr>
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>;
}
echo "</table>";
?>
</body>
</html>
Can you provide the full code for the database connection and query?
Keeping in mind that mysql_* functions have been deprecated in PHP, if you still want to use them, you should be able to do something like this:
<?php
//create connection to MySQL
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password');
//select database
mysql_select_db('mysql_dbname', $link);
//create and execute query
$sql = 'SELECT foo FROM bar WHERE id = 42';
$result = mysql_query($sql, $link);
?>
You should learn how to write the code in a prepared PDO statement. Since MySQL will be deprecated and writing it in MySQL can get you into trouble with injections. This is how you would write this code in a prepared PDO statement. You can write it in MySQLI, but the only thing is you would not be able to use your code with any other databases besides MySQL. With PDO you can use your code with any database like SQL, Access and more.
<?php
$db_host = "localhost";
$db_username = "visitor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
while ($row = $query->fetch()) {
echo $row['code'], " ", $row['name'];
}
?>