Link to Mysql Table
I'm trying to have a basic website display info from a table to use as content. I run this
$result = mysqli_query($conn,"SELECT cont_id, cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
Each page_location is a page on the website. I would like to do something like
<?php echo $row['cont_text'] WHERE cont_id = 15; ?>
I know that's not right but then down the page I would echo cont_text from a new cont_id. How would I go about this?
Do I need to reorganize my table? Or multiple mysqli_query's?
You're over-thinking it. Just run one query using cont_id in the where clause of the query:
$result = mysqli_query($conn,"SELECT cont_text FROM content WHERE cont_id = 15") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
echo $row['cont_text'];
edit
Based on your comments try this:
$result = mysqli_query($conn,"SELECT cont_id , cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$content = array();
while($row = mysqli_fetch_array($result)) {
$content[$row['cont_id']] = $row['cont_text'];
}
echo $content[15];
Related
i want fetch all users data but am getting only one user details, please help me to solve
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql6="SELECT username FROM users";
if($result = mysqli_query($conn, $sql6)){
while ($row=mysqli_fetch_array($result)){
//Hashrate Data Fetch
$investedusername = $row['username'];
$sql3="SELECT sum(hashrate_amount) as total FROM buyhashrate WHERE invested_username='$investedusername'";
$result = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result);
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
Your re-using the $result field, change your second reference to something like ...
$result1 = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result1);
This will stop it reseting the value your using for your main loop in
while ($row=mysqli_fetch_array($result)){
You use the $result variable for both mysqli_query
$result = mysqli_query($conn, $sql3);
While other answers are right about your mistake, I want to give you a better solution.
Try to use a join like this:
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql="SELECT SUM(hashrate_amount) AS total FROM users AS t1 LEFT JOIN buyhashrate AS t2 ON (t1.username=t2.invested_username) GROUP BY t1.username";
if($result = mysqli_query($conn, $sql)){
while ($row=mysqli_fetch_array($result)){
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
This way you do just one query from database. But using your method you have n+1 queries which n is the number of users. So for one hundred users there is 101 queries.
{Connecting 2 tables together (with teacher ID and teacher name)
I have created the join and I have tested in SQL. Seems good. I am trying to print it on the screen.
$classandteacher = "SELECT person_name FROM people RIGHT OUTER JOIN classes ON classes.instructor_id=people.instructor_id ASC";
$result = mysqli_query($dbc, $classandteacher){
while($row = mysqli_query($dbc, $result));
$teacher = $row["person_name"];
echo ("Teacher: " . $teacher . "<br>");}
It's because the While Loop isn't correctly formatted.
Remove the ; and put it into {}
You also need to use mysqli_fetch_assoc($result) to get the Array from the query
$classandteacher = "SELECT person_name FROM people RIGHT OUTER JOIN classes ON classes.instructor_id=people.instructor_id ASC";
$result = mysqli_query($dbc, $classandteacher);
if(!$result) { //If your MYSQL query is throwing an error
error_log(mysqli_error());
}
while($row = mysqli_fetch_assoc($result))
{
$teacher = $row["person_name"];
echo "Teacher: " . $teacher . "<br>";
}
Or try using
mysqli_fetch_array instead
$classandteacher =
"SELECT
person_name FROM
people RIGHT OUTER JOIN classes ON
classes.instructor_id
=
peopleinstructor_id ASC";
$result =
mysqli_query($dbc,$ classandteacher);
if(!$result) { //If
your MYSQL query is
throwing an error
error_log(mysqli_er
ror() );}
while($row =
mysqli_fetch_array(
$result))
{
$teacher = $row
["person_name"];
echo "Teacher: " . $
teacher . "<br>"; }
Very basic but as a rookie I am struggling. The echo doesnt show any value, just the text. What am I doing wrong?
Connect.php:
<?php
$connection = mysqli_connect('test.com.mysql', 'test_com_systems', 'systems');
if (!$connection){
die("Database Connection Failed" . mysqli_error($connection));
}
$select_db = mysqli_select_db($connection, 'swaut_com_systems');
if (!$select_db){
die("Database Selection Failed" . mysqli_error($connection));
}
?>
Get.php:
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
echo ( 'SystemID: '.$result2);
?>
Assuming you have connected to the database successfully then the query is incorrect. You must wrap all text values in quotes like this
<?php
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username='test'";
$result2 = mysqli_query($connection, $query2);
Now the mysqli_query submits the query to the database where it is run and a result set built. To see the result set you need to read the result set back from the database using one of the fetch functions for example
$row = mysqli_fetch_assoc($result2);
echo 'SystemID: ' . $row['systemid'];
If there are more than one rows in the result set you must do that in a loop like this
while ($row = mysqli_fetch_assoc($result2)){
echo 'SystemID: ' . $row['systemid'];
}
You are printing the mysqli result object. In order to printthe result you have to use:
$row = mysqli_fetch_assoc($result2);
print_r($row);
You need to collect the results of the mysqli_query using the following:
require('connect.php');
$query2 = "SELECT systemid FROM user WHERE username=test";
$result2 = mysqli_query($connection, $query2);
while ($row = mysqli_fetch_assoc($result2))
{
echo "System ID is: " . $row['systemid'];
}
Hi I have had a long search for this but have found no fix.
My code is as follows:
$link = mysqli_connect("localhost",".........","...........",".........") or die("Error " . mysqli_error($link));
$ctime = time();
$check = "SELECT * FROM thread WHERE forumid='48' AND visible='1' ORDER BY lastpost DESC LIMIT 1" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc = mysqli_query($link, $check);
while($rows = $rc->fetch_assoc()){
$pid = $rows['firstpostid'];
$query = "SELECT * FROM dropouts WHERE date <= $ctime" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = mysqli_query($link, $query);
$row_cnt = $result->num_rows;
while($row = $result->fetch_array())
{
$date = $row['date'];
$user = $row['username'];
$sql = "DELETE FROM dropouts WHERE date = $date" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$done = mysqli_query($link, $sql);
//////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
$check1 = "SELECT * FROM post WHERE postid='$pid'" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc1 = mysqli_query($link, $check1);
while($row1 = $rc1->fetch_array())
{
$text = $row1['pagetext'];
echo str_replace($user, "", $text);
}
}
}
The problem is that when I run it like that I get no out put.
If I run it so that the while loops are not in eachother I get output but the script only does it for one row post/row.
Does anyone know how to fix this?
I read that it should work but this just isn't working...
Thanks
Have you tried echoing it. It can be that the query returns 0 or empty!
As a beginner in php, you may stumble into these kinds of issues. The best way to understand the problem would be to ECHO & EXIT. Check each step by doing this and you can better understand the problem.
In the problem above, instead of writing the entire code, first of all try checking the small portion of codes and remember:
The best compiler/interpreter lies between you two ears!!
So what I want to do is I want to search for a certain name in a column called 'filename' and display the 'count' on that row.
This might help explain:
I want to search for Packages in the 'filename' column but I want to display the 'count' on it (4)
id filename count
1 Packages 4
2 Another 7
$filename = 'Packages';
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Also note that if the filename can come from user input you have to be aware of injection attacks.
$filename = 'Packages'; // From user
$filename = mysql_real_escape_string($filename);
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Here is an example that selects all Filenames and counts and lists them in a table:
$result = mysql_query("SELECT `filename` `count` FROM `downloads`");
?><table><tr><td>Filename</td><td>Count</td></tr><?php
while($row = mysql_fetch_assoc($result)){
?>
<tr><td><?php echo $row['filename']; ?></td><td><?php echo $row['count']; ?></td></tr>
<?php
}
?></table><?php
Assuming what you actually wants is a report of download counts for each filename, you need to use the GROUP BY clause in your SQL statement:
<?php
$res = mysql_query('SELECT id, filename, COUNT(*) AS count FROM downloads GROUP BY filename');
if (is_resource($res)) {
while (false !== ($row = mysql_fetch_assoc($res))) {
printf('%d - %s - %d', $row['id'], $row['filename'], $row['count']);
}
}
?>
$query = "SELECT count FROM downloads WHERE filename='Packages'";
EDIT:
$result = mysql_query($query, $con) or die(mysql_error());
$arr_down = mysql_fetch_assoc($result);
echo $arr_down["count"];
UPDATE: For one or more rows with 'Packages' in it, you can show the results in a table like this:
$con = mysql_connect("localhost", "MySQL_username", "MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error());
$fname = "Packages";
$query = "SELECT filename, count FROM downloads WHERE filename='" . $fname . "'";
$result = mysql_query($query, $con) or die(mysql_error());
$html = "<table>\n<tr><th>Filename</th><th>Count</th></tr>\n";
while ($row = mysql_fetch_assoc($result))
$html .= "<tr><td>" . $row['filename'] . "</td><td>" . $row['count'] . "</td></tr>\n";
$html .= "</table>\n";
echo $html;
$count = mysql_num_rows($result)
echo($count)
Check out the PHP mysql documentation for more. In particular you'll want to learn about the fetch() functions, which you'll use to display the data you retrieve with your query.
the raw query would be:
select count( filename ) from downloads;
Edit: Okay, you want to loop them onto the page:
foreach( $result as $i )
{
if( !empty( $i ) )
{
echo "<div>$i['column_name']</div>";
}
}