We help IT Professionals succeed at work.

PHP MYSQL QUERY

blknyella
blknyella used Ask the Experts™
on
I'm creating a page that will list usernames in a database using php and mysql. This list put into a drop down list with now errors. Problem is that the query returns the entire row, which i do not want.

EX: username@demo.test.com

How would i query the row and remove everthing after "@"?

TIA
<html>
<head>

<?php
include 'connect.php';

$result = mysql_query("SELECT *, from_unixtime(sentDate/1000) as msgSentDate from ofMessageArchive")
or die(mysql_error());

	$options="";

while($row = mysql_fetch_array( $result )) {

	$id=$row["conversationID"];
	$thing=$row["fromJID"];
	$options.="<OPTION VALUE=\"$id\">".$thing;
}


?>

<SELECT NAME=thing> 
<OPTION VALUE=0>Choose 
<?=$options?> 
</SELECT>

</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
 $thing=$row["fromJID"];
 $thing = substr($thing,0,(strpos($thing,@)-1));

Open in new window


HTH
oops
 $thing = substr($thing,0,(strpos($thing,'@')-1));

Open in new window

you can also use PHP explode


while($row = mysql_fetch_array( $result )) {

	$id=$row["conversationID"];
	$thing=$row["fromJID"];
        $explode = explode($thing,'@');
        $username = $explode[0];//this will return the string before @ from your row
	$options.="<OPTION VALUE=\"$id\">".$username;
}

Open in new window

Author

Commented:
Thanks! I actually had to manipulate it just a bit.

Instead of using

$thing = substr($thing,0,(strpos($thing,'@')-1));

I used:

$thing = substr($thing,0,(strpos($thing,'@')));

I appreciate your help!


Author

Commented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Derokorian's comment http:/Q_27244898.html#36331469
Assisted answer: 0 points for blknyella's comment http:/Q_27244898.html#36331497

for the following reason:

Slight manipulation of the string.

Commented:
Try this :

The line below will have to be changed to suit the array element returned with the email address in, I have just used the same as is used in your example.

$username = strstr($row['conversationID'], '@', true);

Open in new window


<html>
<head>

<?php

include 'connect.php';

$result = mysql_query("SELECT *, from_unixtime(sentDate/1000) as msgSentDate from ofMessageArchive") or die(mysql_error());


echo "<select name=\"thing\">";
while($row = mysql_fetch_array( $result )) {

	$username = strstr($row['conversationID'], '@', true);
	echo "<OPTION VALUE=\"{$username}\">{$row['fromJID']}</option>";
}
echo "</select>";

?>

</body>
</html>

Open in new window

Author

Commented:
Slight manipulation of the string.