473,382 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to get just the string in mysql ?

hello all , i need help at this:
[php] <?
//connection stuff
$conexao = mysql_connect("localhost", "root", "12345")
or die ("error to db.");
$db = mysql_select_db("mal")
or die ("error selecting db.");

// some regex
$subject = $_POST['text'] ;
$pattern = '/def/';
preg_match($pattern, $subject, $matches);
$sql = "INSERT INTO tab1 (nome )
VALUES ('$matches') " ;

$sql = mysql_query($sql)
or die ("there was a mistake");
?>

<form action="mal.php" method="post">
<label for="texto">Texto: </label>
<textarea name="texto" id="text" rows="10" cols="30" />
</textarea><br />

<input type="submit" value="insert">

</form>
[/php]

Ok, lets imagine i put in the text box something like : abcdefg
and i just want : def to be returned to mysql
i dont know why but the only thing i get returned is : array lol
can anyone help?
Jul 31 '07 #1
7 1710
code green
1,726 Expert 1GB
Matches returns an array. You need to extract what you need from the array.

Expand|Select|Wrap|Line Numbers
  1. int preg_match ( string pattern, string subject [, array &matches [, int flags [, int offset]]] )
Searches subject for a match to the regular expression given in pattern.
If matches is provided, then it is filled with the results of search. $matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.
If you are comparing strings without special characters the string functions are quicker.
[PHP]strstr(), strpos()[/PHP]
Jul 31 '07 #2
Sorry , im a newbye here..and i didnt understand what to do... :(
shall i do this?
...
preg_match($pattern, $subject, $matches[1]);
$sql = "INSERT INTO tab1 (nome )
VALUES ('$matches') " ;

...

can someone plese give me a concrete "how-to" put this code running?
Jul 31 '07 #3
code green
1,726 Expert 1GB
No that is wrong. $match returns an array.
ie
[PHP]
$subject = $_POST['text'] ;
$pattern = '/def/';
preg_match($pattern, $subject, $matches);
$text = $match[0];
$sql = "INSERT INTO tab1 (nome )
VALUES ('$text') " ;[/PHP]
But this makes no sense.
Why are you returning a string that you already know the value of?
Why not just send $pattern to the DB?
Jul 31 '07 #4
this was just 1 example , i just want to understand how to input that string :)
the real code will be something like this :
[php]
<?
$conexao = mysql_connect("localhost", "root", "12345")
or die ("Erro na conexão ao banco de dados.");
$db = mysql_select_db("mal")
or die ("Erro ao selecionar a base de dados.");


if( preg_match ('/(.+) \(ok\)\n/' , $_POST['texto'], $matches ) )
{
$sql = "INSERT INTO tabela1 (nome )
VALUES ($matches) " ;

if (!$result = @mysql_query($sql))
{
exit(mysql_error());
}
}

?>



<form action="mal.php" method="post">
<label for="texto">Texto: </label>
<textarea name="texto" id="texto" rows="10" cols="30" />
</textarea><br />

<input type="submit" value="inserir">

</form>
[/php]
and the things that users will input in the text box its something like : Jonh (ok) , greg (ok).
Jul 31 '07 #5
and btw, i tested your ideia, but still, just get the array word in mysql
Jul 31 '07 #6
kovik
1,044 Expert 1GB
and btw, i tested your ideia, but still, just get the array word in mysql
Then do something about it. There are dozens are array functions, and you could turn an array into a string using a combination of any of them. A simple approach is the implode().
Aug 1 '07 #7
code green
1,726 Expert 1GB
and btw, i tested your ideia, but still, just get the array word in mysql
You obviously did not test my idea, you copied my text and hoped it would work. If you look again you can see I made a mistake.
this was just 1 example
Yes I knew that.
But I really think you are getting bogged down with preg_match when the string functions would be better.
Regular expressions are only necessary when dealing with HTML, XML for example..
If I am right, you want to extract the text occuring before the word 'ok'.
You can use a combination of strpos and substr for this
[PHP]$matches = substr($_POST['texto'],strpos($_POST['texto'],'ok' ))[/PHP]This may require some manipulation.
You may need to add the length of the string 'ok' to the value returned by strpos.
Aug 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Wayne... | last post by:
I'm trying to get asp to conect to a MySQL database, I've tried copying a few examples online that I found and I always get a 500 error with the code below. what am i doing wrong? any examples of...
17
by: AMC | last post by:
Hi, I'm using an include file to store the connection string to a database. Whenever I try to reference that string to open a connection in the page that includes the file I get the error 'empy...
0
by: Joe | last post by:
Hi, I am trying to connect to a MySQL database through a Java Servlet. I think I have correctly installed Tomcat 4.1.30 and the MySQL Database along with ConnectorJ. I am getting a...
5
by: MLH | last post by:
I'm supposed to set a password for the MySQL root user. The output of mysql_install_db instructed me to run the following commands... /usr/bin/mysqladmin -u root -h appserver password mynwewpasswd...
5
by: Aries | last post by:
I have a connection string like this, anyone know how can I fix it? <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <html> <body> <!-- #include file =...
1
by: Lorenzo | last post by:
I have a problem with oledb connection string of Mysql for .NET I installed MyOleDB3.exe Mysql server version 4.1.8-nt I used this connection string : ...
2
by: zeljko.prince | last post by:
This is a copy from http://forums.mysql.com/read.php?10,73797,73797#msg-73797. Perhaps someone on this group will know the answer. Given the following table: CREATE TABLE foo(field VARCHAR(20)...
66
by: mensanator | last post by:
Probably just me. I've only been using Access and SQL Server for 12 years, so I'm sure my opinions don't count for anything. I was, nevertheless, looking forward to Sqlite3. And now that gmpy...
2
by: Hetal | last post by:
Hi... I am a newbie VB.NET developer and i am looking at working with ADO.NET rather than ADO. In one of our native VB application with ADO, we used to create 1 connection object and that would...
8
by: Tony B | last post by:
I have a string in a existing php script which is in the form "dd/mm/yyyy" and I need to convert it into a suitable format for mysql which is "yyyy-mm-dd" Is there a neat way of doing this in php ?
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.