I have a simple query which works correctly in phpMyadmin, however doesn't when I echo it out on a page:
$query = "SELECT * FROM PEOPLE WHERE MEMBER_TYPE Like 'LFM' ORDER BY LAST_NAME ASC ";
}
$query = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($query)){...`
For unknown reasons it simply will not sort the results by the last name column from within the table. Any ideas why this could be?
Related
I've been trying to display the number of rows using this code but it keep says
1 Rows which is wrong
<?php
$link = mysql_connect("localhost", "gamin1_forum", "password123");
mysql_select_db("gamin1_forumdb", $link);
$result = mysql_query("SELECT COUNT(*) FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows";
?>
Rows are approximately 1443 but it kept saying 1
The Count(*) returns you one row which contains the number of rows as a value.
By using mysql_num_rows($result) you are actually counting the amount of rows of the Count(*) result which really is one.
Change it to:
$result = mysql_query("SELECT * FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
Or just use the Count(*) value (which is probably better since it count in the DB and not retrieving the whole table for it) using mysql_fetch_array.
This is true, mysql_num_rows() is telling that there was one "row" returned. You need to check the value of the returned row to get your count. Try this
$link = mysql_connect("localhost", "gamin1_forum", "password123");
$result = mysql_query("SELECT COUNT(*) as cnt FROM smf_personal_messages", $link);
$row = mysql_fetch_array($result);
echo ($row['cnt']);
On a side note, you should not use mysql_* functions for security reasons. Try PDO/mysqli_* functions
I have a column in my MySQL database for deleted values are usually 0 or 1. Usually when doing searches I omit the deleted by doing something like "and deleted = "0"" but I cant figure out how to get this query below to omit my deleted column. any ideas would be appreciated thanks!
$query = "SELECT Status, COUNT(Status) FROM Assets GROUP BY Status";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result))
{
echo "". $row['COUNT(Status)'] ." systems ". $row['Status'];
echo ", ";
}
This should work:
$query = "SELECT Status, COUNT(Status) FROM Assets WHERE Assets.deleted = 0 GROUP BY Status";
Also, you should use mysqli or PDO rather than mysql (the php mysql library is obsolete).
here is the code that i am using, the code is simply getting the same value by two different methods, but the mysql_insert_id() echo is not giving the output.
The value given by -> echo mysql_insert_id(); is 0
Can anyone tell me the possible reason and how to resolve this problem?
include'config.php';
$query = "SELECT id
FROM users
ORDER BY id DESC
LIMIT 1";
if($query_run = mysql_query($query))
{
$query_result = mysql_fetch_row($query_run);
$id = $query_result[0];
echo $id;
echo $lastId = mysql_insert_id().'<br />';
//'Query Successful!';
} else {
echo mysql_error();
}
If you are looking for the next auto increment id that will be inserted. I guess you should try this.
$query_autoinc="SHOW TABLE STATUS LIKE 'mytable'";
$exec_autoinc=mysql_query($query_autoinc);
$row_autoinc=mysql_fetch_assoc($exec_autoinc);
$inserted_id = $row_autoinc['Auto_increment'];
Or if you want the last inserted id, you might try this.
$query= "SELECT max(id)
FROM users
ORDER BY id DESC
LIMIT 1";
I have the following query, and would like to list only the first match.
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
(?category=b)
So DISTINCT could do this right? This is what I tried, but did not work:
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
EDIT: Here is the full code:
function getCategory() {
$first = $_GET['category'];
$first = $first[0] . "%";
$query = mysql_query("SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'") or die(mysql_error());
//$query = mysql_query("SELECT * FROM lyrics WHERE authorclean LIKE '".$first."'") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "Geen resultaten gevonden.";
} else {
while ($row = mysql_fetch_assoc($query)) { ?>
<p><?= $row['author']; ?></p>
<?php }
}
}
(B% is just for testing)
If I run this following query in the database directly I get two results. If I run with the code above I just get an empty page (except for the html thats already there).
SELECT DISTINCT authorclean FROM lyrics WHERE authorclean LIKE 'B%'
You should use LIMIT 1 to list only the first match.
If you have a a table "tbl_lyrics" with fields: author lyrics year and is filled for example as follows:
author_A lyrics_A year_A
author_A lyrics_A1 year_A1
author_A1 lyrics_A2 year_A
author_B lyrics_B1 year_B1
if you do
select distinct(author) from tbl_lyrics where author like '%author_A%'
you are going to get: author_A and author_A1. NOT the first one that matches.
If you want the first one that matches you can do:
select author from (select distinct(author) as author from tbl_lyrics where author like '%author_A%') where rownum <2;
this will return author_A only.
Limit is used with MySql but would not work with oracle databases
I need to connect to a MySQL database and then show the number of rows. This is what I've got so far;
<?php
include "connect.php";
db_connect();
$result = mysql_query("SELECT * FROM hacker");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
When I use that code I end up with this error;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\username\Desktop\xammp\htdocs\news2\results.php on line 10
Thanks in advance :D
You would probably be better of asking the database to aggregate the number of rows instead of transferring them all to php and doing the counting there.
SELECT COUNT(*) FROM hacker
take a habit to run all queries this way:
$sql = "SELECT * FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
and you will always have comprehensive error information
and take appropriate corrections
also, as it was mentioned above, the only reliable way to count rows is SELECT count(*) query
$sql = "SELECT count(*) FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$count = $row[0];
change your code as following:
$result = mysql_query("SELECT * FROM hacker");
echo mysql_error();
You have an SQL-Error or your not connected to the database